将数据附加到二进制文件fstream [英] appending data to binary file fstream

查看:71
本文介绍了将数据附加到二进制文件fstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法将字符串附加到二进制文件的末尾。我正在使用

以下代码:


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.

推荐答案

rory写道:
rory wrote:

我似乎无法将字符串附加到二进制文件的末尾。我正在使用

以下代码:


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指针定位到二进制文件的末尾但是看起来好像没有
工作。任何帮助将不胜感激。干杯,
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,



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.


12月31日凌晨3:25,Barry< dhb2 ... @ gmail.comwrote:
On Dec 31, 3:25 am, Barry <dhb2...@gmail.comwrote:

rory写道:
rory wrote:

我似乎无法将字符串附加到二进制文件的末尾。我正在使用

以下代码:
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();
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指针定位到二进制文件的末尾但是看起来好像没有
工作。任何帮助将不胜感激。干杯,
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,



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.


rory写道:
rory wrote:

12月31日凌晨3:25,Barry< dhb2 ... @ gmail.comwrote:
On Dec 31, 3:25 am, Barry <dhb2...@gmail.comwrote:

> rory写道:
>rory wrote:

>>我似乎无法将字符串附加到二进制文件的末尾。我正在使用以下代码:
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并将指针定位到二进制文件的末尾,但这似乎也无法工作。任何帮助将不胜感激。干杯,
>>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,


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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆