删除文件后,文件打开问题。在vc ++中fopen状态失败 [英] File open issue, after file is deleted. fopen status is failing in vc++

查看:188
本文介绍了删除文件后,文件打开问题。在vc ++中fopen状态失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fopen示例* /

fopen example */

#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
  FILE * pFile;
  for(int i=0; i &lt; 1000000; i++)
  {
            bool ret = remove("C:\\abc.txt");
            pFile = fopen ("C:\\abc.txt","w");
            if (pFile!=NULL)
            {                     
                        fputs ("fopen example",pFile);
                        fclose (pFile);
            }
            else
            {
                        printf("%d fopen() fails \n", count);
            }
  }
  return 0;
}



这里,在调用remove之后,调用pFile = fopen(C:\\abc.txt,w); ,

有时即使在C:\中,删除被调用后abc.txt文件也不存在,但是fopen pFile指针为空。

这是有时再现的总是。在这个例子中,这个问题被复制了50/60次。

请建议一些解决方案。



代码块添加 - OriginalGriff [ / edit]


Here, after remove is called, pFile = fopen ("C:\\abc.txt","w");is called,
Sometimes even in C:\ the abc.txt file is not present after remove called, but the fopen pFile pointer is null.
This is reproduced some times not always. In this example this issue is reproduced 50/60 times.
Please suggest some solution.

[edit]Code block added - OriginalGriff[/edit]

推荐答案

你使用的函数没有理由期待一个问题,所以我不能说是什么导致了这个问题。 (虽然Jochens的建议 - 一个缓存问题 - 似乎有道理)



但是,如果你打开文件进行写作,根本不需要调用删除:如果文件已经存在,它将被清空并视为新文件。请参见 http://www.cplusplus.com/reference/cstdio/fopen/ [ ^ ]
The functions you use give no reason to expect an issue, so I can't say what causes this. (although Jochens suggestion - a caching issue - seems plausible)

However, there is no need to call remove at all if you open the file for writing anyway: if the file already exists, it will be emptied and treated as new. See http://www.cplusplus.com/reference/cstdio/fopen/[^]


试试这个:



http://msdn.microsoft.com/en-us/library/windows/desktop/bb773584(v = vs.85).aspx [ ^ ]


这篇关于删除文件后,文件打开问题。在vc ++中fopen状态失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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