C++ 这个项目赢得了';t以二进制形式读取INT并将其以二进制形式输出到新文件

C++ 这个项目赢得了';t以二进制形式读取INT并将其以二进制形式输出到新文件,c++,iostream,C++,Iostream,我有6个整数 25 5. 30 6. 20 36 在txt文件中。程序会打开txt文件ok。在我的main“cout for loop”中,程序输出看起来是整数的二进制表示形式。但是,输出文件包含相同的整数,而不是二进制数据。我从课本上得到了这个样本。如何修复它以读取整数并写出二进制整数 #include "lib.h" //======================================================================================

我有6个整数 25 5. 30 6. 20 36 在txt文件中。程序会打开txt文件ok。在我的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();

}
//========================================================================================
//========================================================================================
#包括“lib.h”
//========================================================================================
//========================================================================================
无效开放填充(ifstream和ifs)
{
字符串填充;
填充;
ifs.open(infle.c_str(),ios_base::binary);
如果(!ifs)错误(“无法打开文件”);
}
//========================================================================================
//========================================================================================
空旷的排水口(流和ost)
{
排管器;
输出文件;
ost.open(outfile.c_str(),ios_base::binary);
如果(!ost)错误(“无法打开文件”);
}
//========================================================================================
//========================================================================================
void get_fileContent(ifstream&ifs、vector&v)
{
int i=0;
而(ifs.read(作为字节(i),sizeof(int)))
v、 推回(i);
}
//========================================================================================
//========================================================================================
无效写入文件内容(流和ost、向量和v)
{
对于(int i=0;ivoid write_文件内容(流和ost、向量和v)
{
对于(int i=0;i(&v[i]),sizeof(int));
}
}

这应该是可行的。

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

void write_fileContent(ofstream& ost, vector<int>& v)
{
    for (int i = 0; i < v.size(); ++i)
    {   
        ost << endl << v[i]<< endl;
    }
}
void write_fileContent(流&ost、向量&v)
{
对于(int i=0;iost您认为二进制文件是什么?当您在文本编辑器中打开它时,它会显示0和1?您首先是如何编写此二进制文件的?您应该使用
write\u fileContent
方法您错了,输出文件确实包含二进制数据。但您的二进制数据恰好是文本数据。您正在编写如果输入是文本文件,为什么打开时要传递
ios\u base::binary
?我不确定你的意思是“使用写入文件内容”?我写了这个函数,我认为它不是行业标准的“方法”。确实如此!!但现在文件包含:168637746 856296757 906628400 808585741 909314573我可以创建另一个程序,将其转换回整数形式,然后将整数输出到新文件吗?@user2904033,这开始感觉像是XY问题,您在这里试图解决什么问题?@shuttle87创建一个程序,该程序读取以整数表示,并写出二进制表示。创建第二个程序,使相同的步骤颠倒。
void write_fileContent(ofstream& ost, vector<int>& v)
{
    for (int i = 0; i < v.size(); ++i)
    {   
        ost << endl << v[i]<< endl;
    }
}