2013-12-08 3 views
2

텍스트 파일에 쓰여진 이진수를 읽고 이진수를 다른 파일로 변환하는 프로그램을 만들려고하지만이 번호를 이미 쓰는 방법을 알아낼 수 없습니다. "BinaryInputFile.txt"변환 된 함수를 사용하여. 에C++이 파일을 읽고 데이터를 변환합니다.

myfile << "Converted binary number is: " << Binary(line.c_str());

- : 당신이 정확히 하나 하나 "BinaryInputFile.txt"의 진수와

당신은 다음과 같이 당신의 Binary 기능에 std::basic_string::c_str를 사용할 수있는 다른 문자를 한 경우

#include <iostream> 
#include <bitset> 
#include <string> 
#include <fstream> 
#include <math.h> 

int Binary(const char* binary); 

using namespace std; 

int main() 
{ 
char* Num = "1010"; 
string line; 



    ifstream myfile ("BinaryInputFile.txt"); 
    if (myfile.is_open()){ 

    while (getline (myfile,line)){ 
     cout << "Binary number read from the file is: " << line << '.\n'; // display line 
    // printf("%d\n",Binary(line)); 

    } 


    ofstream myfile ("BinaryConvert.txt"); 
     myfile << "Converted binary number is: " << '.\n'; 
     myfile.close(); //close file 
    } 
    else cout << "Unable to open file"; 




    return 0; 
} 


int Binary(const char* binary) //conversion 
{ 
    int len,dec=0,i,exp; 

    len = strlen(binary); 
    exp = len-1; 

    for(i=0;i<len;i++,exp--) 
     dec += binary[i]=='1'?pow(2,exp):0; 
    return dec; 
} 
+1

무엇이 문제입니까? –

+0

"int Binary (const char * binary);"함수를 사용하여 텍스트 파일에서 이진수를 변환하는 방법을 알아낼 수 없습니다. – ssj3goku878

+0

당신이 주석 처리 한'printf' 문은 올바른 일을하는 것처럼 보입니다. 그게 뭐니 뭐 잘못한거야? –

답변

2

아래 코드 쓰기 : "BinaryConvert.txt"

관련 문제