У меня 6 ints 25 5 30 6 20 36 в txt файле. Программа открывает файл txt в порядке. В моем "cout for loop" в основном программа выводит то, что представляется двоичными представлениями int. Но выходной файл содержит извлечение таких же ints, а не двоичных данных. Я получил этот образец из своего учебника. Как я могу исправить это, чтобы читать в ints и записывать двоичные ints?
#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();
}
//========================================================================================
//========================================================================================
Выходной сигнал из циклы for в основном: 168637746 856296757 906628400 808585741 909314573
Если вы хотите, чтобы ваш файл выглядел как ваш вывод в 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) );
}
}
Это должно сработать.
write_fileContent