fopen函数a+方式创建文件失败,路径正确

卑鄙的我 2012-07-11 10:28:15
系统:win2003r2
跟进fopen函数以后失败在这一句代码上if ((stream = _getstream()) == NULL){return NULL}, 返回了NULL。
文件创建的路径是没有问题的。
什么原因 ?
...全文
938 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
snake_pass 2014-08-07
  • 打赏
  • 举报
回复
没有fclose吧?
liuyunhuanying 2013-06-09
  • 打赏
  • 举报
回复
_getstream()方法是试图找到一个没有被使用的stream去打开文件,如果达到512个并且没有找到可用的stream,就会返回NULL。 查找一下你的代码,是不是过多的使用了打开文件,并且没有释放handle。
蓝鹰 2012-07-25
  • 打赏
  • 举报
回复
换成绝对路径试一试,
卑鄙的我 2012-07-25
  • 打赏
  • 举报
回复
最终也没找到原因,可能是内部逻辑出错了吧。。。
赵4老师 2012-07-11
  • 打赏
  • 举报
回复
"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.

When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

zhanshen2891 2012-07-11
  • 打赏
  • 举报
回复
1. 改成这样试试 。.\\AntiPlugin\\APLog\\%d-%d-%d\\记录.txt
2. 把路径里的中文去掉试试
卑鄙的我 2012-07-11
  • 打赏
  • 举报
回复
这个程序从昨天开到今天,昨天创建的AntiPlugin\APLog\2012-7-10\记录.txt成功了,本来今天来了就应该看见AntiPlugin\APLog\2012-7-11\记录.txt,却只有AntiPlugin\APLog\2012-7-11目录,记录.txt文件却一直没有创建成功,跟进fopen函数以后失败在if ((stream = _getstream()) == NULL){return NULL}。
卑鄙的我 2012-07-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

文件是空的?_getstream()是做什么的
[/Quote]

文件还没创建 , 目的就是要用fopen函数创建文件, 进入fopen函数以后在if ((stream = _getstream()) == NULL){return NULL}代码返回了、
卑鄙的我 2012-07-11
  • 打赏
  • 举报
回复
代码:
char FileName[258] = {0};
SYSTEMTIME sysTime;
GetLocalTime(&sysTime);
//根据当前时间来格式化目录名与文件名
sprintf(FileName,"AntiPlugin\\APLog\\%d-%d-%d\\记录.txt",sysTime.wYear, sysTime.wMonth, sysTime.wDay);

//如果打开文件失败就创建目录
FILE* pFile = fopen(FileName,"a+");
if ( pFile == NULL )
//遍历创建目录
ReserveDir( FileName );


//创建完成目录后如果再次打开失败则返回 不做后续写入
pFile = fopen( FileName, "a+" );
if ( pFile == NULL )
return 0;

以上代码文件目录是创建成功了的,并且通过改系统时间文件也能被创建成功,但是今天由于一个未知原因失败在fopen函数中的这一句代码if ((stream = _getstream()) == NULL){return NULL}。
pathuang68 2012-07-11
  • 打赏
  • 举报
回复
如果楼主确认路劲没有问题,那应该就是_getstream()这个函数有问题了,单步调试这个函数。
W170532934 2012-07-11
  • 打赏
  • 举报
回复
文件是空的?_getstream()是做什么的
HandanXiaoliang 2012-07-11
  • 打赏
  • 举报
回复
用别的方式打开现存的文件,也失败吗
蓝鹰 2012-07-11
  • 打赏
  • 举报
回复
代码贴上来啊

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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