问题描述
我似乎无法将字符串附加到二进制文件的末尾。我正在使用
以下代码:
fstream outFile(" test.exe",ios :: in | ios :: out | ios :: binary |
ios :: ate | ios :: app)
outFile.write(" teststring",10);
outFile.close();
如果我省略了ios :: ate和ios :: app模式,我的字符串将被写入
的开头文件正如我所料,但我想将数据写入
二进制文件的结尾而不是开头。谁能告诉我
我做错了什么?我也尝试过使用seekp并将
put指针定位到二进制文件的末尾但是看起来好像没有
工作。任何帮助将不胜感激。干杯,
Rory。
I can''t seem to append a string to the end of a binary file. I''m using
the following code:
fstream outFile("test.exe", ios::in | ios::out | ios::binary |
ios::ate | ios::app)
outFile.write("teststring", 10);
outFile.close();
If I leave out the ios::ate and ios::app modes my string is written to
the start of the file as I''d expect but I want to write the data to
the end of the binary file not the beginning. Can anyone advise me on
what I''m doing wrong? I''ve also tried using seekp and positioning the
put pointer to the end of the binary file but that doesn''t seem to
work either. Any help would be greatly appreciated. Cheers,
Rory.
推荐答案
27.8.1.3/2
表92?文件打开模式
依据在桌子上,app当在...时,永远不会与in
一起使用和外出放在一起,就意味着我们要更新旧的
数据,在你的情况下,你并不是故意的。
所以使用:ios_base ::二进制| ios_base :: out | ios_base :: app
如果你使用fostream,那么out默认情况下。
27.8.1.3/2
Table 92?File open modes
according to the table, "app" never goes with "in"
when "in" and "out" are put together, it means we want to update the old
data, in your case, you didn''t mean to.
so use: ios_base::binary | ios_base::out | ios_base::app
if you use fostream, then "out" is by default.
27.8.1.3/2
表92 - 文件打开模式
根据表格,app当在...时,永远不会与in
一起使用和外出放在一起,就意味着我们要更新旧的
数据,在你的情况下,你并不是故意的。
所以使用:ios_base ::二进制| ios_base :: out | ios_base :: app
如果你使用fostream,那么out是默认情况下。
27.8.1.3/2
Table 92--File open modes
according to the table, "app" never goes with "in"
when "in" and "out" are put together, it means we want to update the old
data, in your case, you didn''t mean to.
so use: ios_base::binary | ios_base::out | ios_base::app
if you use fostream, then "out" is by default.
感谢您的回复。我试过了,但它仍然没有写任何东西
到文件的末尾。我现在已经尝试了几乎所有我能想到的并且现在仍然喜悦的
模式的组合。我知道如果我不能理解它我可以很容易地用C实现它但是我之前有过这个问题
并且很想找到一个解。再次感谢您的帮助,
Rory。
Thanks for you reply. I tried that but it still doesn''t write anything
to the end of the file. I have now tried almost every combination of
modes I can think of and still now joy. I know if I can''t figure it
out that I can easily implement it in C but I''ve had this problem
before and would love to find a solution. Thanks again for your help,
Rory.
27.8.1.3/2
表92 - 文件打开模式
根据表格,app当在...中时,永远不会与in相关。
和外出放在一起,就意味着我们想要更新旧的数据,在你的情况下,你并不是故意的。
所以使用:ios_base :: binary | ios_base :: out | ios_base :: app
如果你使用fostream,那么out是默认情况下。
27.8.1.3/2
Table 92--File open modes
according to the table, "app" never goes with "in"
when "in" and "out" are put together, it means we want to update the old
data, in your case, you didn''t mean to.
so use: ios_base::binary | ios_base::out | ios_base::app
if you use fostream, then "out" is by default.
感谢您的回复。我试过了,但它仍然没有写任何东西
到文件的末尾。我现在已经尝试了几乎所有我能想到的并且现在仍然喜悦的
模式的组合。我知道如果我不能理解它我可以很容易地用C实现它但是我之前有过这个问题
并且很想找到一个解。再次感谢您的帮助,
Thanks for you reply. I tried that but it still doesn''t write anything
to the end of the file. I have now tried almost every combination of
modes I can think of and still now joy. I know if I can''t figure it
out that I can easily implement it in C but I''ve had this problem
before and would love to find a solution. Thanks again for your help,
您是否检查过您是否成功打开文件?
have you check if you opened the file successfully?
这篇关于将数据附加到二进制文件fstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!