如何将字符串写入二进制文件
使用这段代码,我尝试打印字符串“foo”10次 以二进制格式。 但为什么执行此操作的函数不起作用呢?
#include <iostream>
#include <fstream>
using namespace std;
template <typename T> void WriteStr2BinFh (string St, ostream &fn) {
for (unsigned i = 0; i < St.size(); i++) {
char CStr = St[i];
fn.write(&CStr.front(), CStr.size());
}
return;
}
int main() {
string MyStr = "Foo";
ofstream myfile;
myfile.open("OuputFile.txt", ios::binary|ios::out);
// We want to print it 10 times horizontally
// separated with tab
for (int i = 0; i < 9; i++) {
WriteStr2BinFh(Mystr+"\t", myfile);
}
myfile.close();
}
With this code I tried to print the string "foo" 10 times
in binary format. But why doesn't the function to do it work?
#include <iostream>
#include <fstream>
using namespace std;
template <typename T> void WriteStr2BinFh (string St, ostream &fn) {
for (unsigned i = 0; i < St.size(); i++) {
char CStr = St[i];
fn.write(&CStr.front(), CStr.size());
}
return;
}
int main() {
string MyStr = "Foo";
ofstream myfile;
myfile.open("OuputFile.txt", ios::binary|ios::out);
// We want to print it 10 times horizontally
// separated with tab
for (int i = 0; i < 9; i++) {
WriteStr2BinFh(Mystr+"\t", myfile);
}
myfile.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有很多错误,我只想列出我看到的所有内容:
您的 for 循环条件应该是 i < 10.
为什么使用模板而不使用模板化参数T?
您在 CStr 上调用方法 front() ,但 CStr 是一个字符,而不是字符串,所以我什至不知道它是如何编译的。
假设 CStr 是一个字符串,您不想使用 & 获取 front() 迭代器的地址,而是想这样说:
并且您不想循环 St.size() 迭代。 只需执行上述操作即可。
There is so much wrong here, I'm just going to list everything I see:
Your for loop condition should be i < 10.
Why are you using a template but not the templatized parameter T?
You're calling the method front() on CStr, but CStr is a char, not a string, so I don't even know how that compiles.
Assuming CStr was a string, you don't want to take the address of the front() iterator using &, instead you want to say something like:
And you don't want to loop for St.size() iterations. Just do the above.
天哪,它有很多错误:
使用代码组织和命名修复了您的示例:
但我建议使用
std::fill_n
算法omg, it have a lot of errors:
fixed your example, with your code organize and your naming:
but I've reccomended to use
std::fill_n
algorithm首先,
char CStr
表示CStr
是单个字符。 其次,fn.write(&CStr.front(), CStr.size());
将该字符视为容器,如std::vector
,这当然无法编译。假设
WriteStr2BinFh
之前的一切都正常,我还没有检查过,这就是WriteStr2BinFh
应该(可能)看起来的样子:或者,最好是
First,
char CStr
says thatCStr
is a single character. Second,fn.write(&CStr.front(), CStr.size());
treats that character as a container, likestd::vector<>
, which of course cannot compile.Assuming that everything up to
WriteStr2BinFh
is ok, which I haven't checked, this is howWriteStr2BinFh
should (could) look:or, preferably
在二进制模式下执行 io 操作的要点:
文件必须在最后关闭。
此 I/O 二进制函数将一些数据写入该函数。
文件使用 ios::out 和 ios::binary 以输出和二进制模式打开。
还有一个说明符 ios::app,它告诉操作系统该文件也是以附加模式打开的。 这意味着任何新的数据集都将附加到文件末尾。
上面使用的write函数,需要参数为字符指针类型。 因此我们使用类型转换器reinterpret_cast将结构体类型转换为char*类型。
Important points for doing an io operation in binary mode:
File has to be closed at the end.
This I/O binary function writes some data to the function.
The file is opened in output and binary mode with ios::out and ios::binary.
There's one more specifier ios::app, which tells the Operating system that the file is also opened in append mode. This means any new set of data will be appended to the end of file.
The write function used above, needs the parameter as a character pointer type. So we use a type converter reinterpret_cast to typecast the structure into char* type.