此项目不会将整数读取为二进制并将其作为二进制输出到新文件

this project won't read ints as binary and output them as binary to a new file

本文关键字:二进制 输出 新文件 文件 项目 整数 读取      更新时间:2023-10-16

我有 6 个整数2553062036在 TXT 文件中。程序打开 txt 文件正常。在我的 main 中的"cout for loop"中,程序输出似乎是整数的二进制表示。但是,输出文件包含提取相同的整数,而不是二进制数据。我从我的教科书中得到了这个样本。如何修复它以读取整数并写出二进制整数?

#include "lib.h"
//========================================================================================
//========================================================================================
 void open_infile(ifstream& ifs)
{
    string infile;
    cout << "Please enter the name of the file:";
    cin >> infile;
   ifs.open(infile.c_str(), ios_base::binary);
    if (!ifs) error("can't open out file");
}
//========================================================================================
//========================================================================================
 void open_outfile(ofstream& ost)
{
    string outfile;
    cout << "Please enter the name of the file:";
    cin >> outfile;
    ost.open(outfile.c_str(), ios_base::binary);
    if (!ost) error("can't open out file");
}
//========================================================================================
//========================================================================================
 void get_fileContent(ifstream& ifs, vector<int>& v)
{
    int i = 0;
    while(ifs.read(as_bytes(i),sizeof(int)))
        v.push_back(i);
}
 //========================================================================================
//========================================================================================
 void write_fileContent(ofstream& ost, vector<int>& v)
{
    for(int i  =0; i < v.size(); ++i)
        {   
        ost.write(as_bytes(v[i]),sizeof(int));
        }
}
//========================================================================================
//========================================================================================
int main()
{
    ifstream ifs;
    ofstream ost;
    vector<int> v;
    open_infile(ifs);
    get_fileContent(ifs, v);

    //just checking to make sure data is being copied to int vector correctly
    for(int i  =0; i < v.size(); ++i)
        {   
        cout<< endl << v[i]<< endl;
        }
    open_outfile(ost);
    write_fileContent(ost, v);
    keep_window_open();
}
//========================================================================================
//========================================================================================

main 中 for 循环的输出为:168637746856296757906628400808585741909314573

如果您希望您的文件看起来像您的输出到 stdout,那么您需要像对 cout 一样编写它:

void write_fileContent(ofstream& ost, vector<int>& v)
{
    for (int i = 0; i < v.size(); ++i)
    {   
        ost << endl << v[i]<< endl;
    }
}

operator<<执行格式化输出,而write执行未格式化输出。

void write_fileContent(ofstream& ost, vector<int>& v)
{
    for(int i  =0; i < v.size(); ++i)
    {   
        ost.write( reinterpret_cast< char* >( &v[i] ), sizeof(int) );
    }
}

这应该有效。