此项目不会将整数读取为二进制并将其作为二进制输出到新文件
this project won't read ints as binary and output them as binary to a new file
我有 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) );
}
}
这应该有效。
相关文章:
- 二进制搜索不适用于特定输出
- 输入 1024 后十进制到二进制转换的输出错误?
- 读取二进制文件时如何输出?
- 编译器将输出的流运算符<<解释为用于按位左移的二进制运算符<<
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- qDebug - 如何以位(二进制格式)输出数据
- 在C 中输出到二进制文件中的麻烦
- 如何获取与 objdump 输出的标签相对应的 ELF 二进制文件中的文件偏移量
- 二进制搜索 - 代码编译并运行后不显示输出
- Z3 C++ API 产生"unknown",而序列化输出上的二进制生成"unsat"
- 具有特征类型输出的特征二进制 Expr
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 我如何知道谷物二进制输出的大小?
- 二进制搜索树的输出不正确
- 二进制输出到C++中随机位置的文件
- 此项目不会将整数读取为二进制并将其作为二进制输出到新文件
- 为什么 std::setbase(2) 不切换到二进制输出?
- 在不截断的情况下打开二进制输出文件流
- 如何使用 g++ 生成确定性的二进制输出
- g++二进制输出分析