Need to convert txt file into binary file in C++

前端 未结 8 963
你的背包
你的背包 2021-01-06 23:32

I have a txt file with numbers like 541399.531 261032.266 16.660 (first line) 541400.288 261032.284 16.642 (2nd line)........hundred of points. i want to convert this file i

8条回答
  •  迷失自我
    2021-01-07 00:30

    This is what you may want to do.

    1. Open the text file using ifstream.
    2. Open the target file using ofstream in binary mode.
    3. Read information from file1 and write to file2.

    Some sample code (untested):

        ifstream ifile("file1.txt");  
        ofstream ofile("file2.txt", ios::binary);  
        string line;  
        while(!ifile.eof()) {  
        getline(ifile, line);  
        ofile.write(line.c_str(), line.length);  
       }
    

    HTH,
    Sriram

提交回复
热议问题