在写文件时第一次写入时能实现 ,但删除后再写入的时候就实现不了,为什么?

ruiyiiuo2007 2007-09-04 03:25:07

在写文件时第一次写入时能实现 ,但删除后再写入的时候就实现不了,为什么?
例:
FILE *fp = NULL;
fp = fopen("top.txt","b+");
if(fp)
{
fwrite(szSqlBuffer888,strlen(szSqlBuffer888),1,fp);
fclose(fp);

}
...全文
162 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruiyiiuo2007 2007-09-05
  • 打赏
  • 举报
回复
多谢谢呀.
xiao_ke 2007-09-04
  • 打赏
  • 举报
回复
是啊 你那個b+ 。。。。。。。
KenYuan2016 2007-09-04
  • 打赏
  • 举报
回复
Please choose the correct parameter in your code...

"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.
zywhuiss 2007-09-04
  • 打赏
  • 举报
回复
"fp = fopen("top.txt","b+");"
怎么MSDN里面对fopen的说明终没有"b+"这个参数?楼主认真看过MSDN吗?
冷月清晖 2007-09-04
  • 打赏
  • 举报
回复
多些判断 错误处理

if(NULL==(fp = fopen("top.txt","b+")))
/*文件没打开,报错*/
else
/*操作*/
if( 0==fclose( fp ) )
/*文件关闭*/
else
/*报错*/
冷月清晖 2007-09-04
  • 打赏
  • 举报
回复
当然啦,关闭之后要再次打开呀
IfI 2007-09-04
  • 打赏
  • 举报
回复
用c++的话建议用标准c++的库.

这个例子中,你把文件删除了,却没有指定"如果文件不存在就创建它"的参数,所以获取文件指针失败.

按楼上说法,换成wb+试试.
awperpvip 2007-09-04
  • 打赏
  • 举报
回复
debug一下,fp始终为NULL
fopen失败,改一下打开方式,比如传参数为wb+试下OK.

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧