文件未在C中删除 [英] File not deleted in C

查看:76
本文介绍了文件未在C中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除文件的包含...但不删除\\ n \\ n> b $ b

I tried to delete the contains of a file ...but not deletetd

unsigned char buffer[64];	// buffer may be any size
FILE *fp = fopen("image.pgm", "rb";);
FILE *pFile;
char *filename="text";
int ret;
int numread;
 pFile=fopen(filename,"wb");
while (!feof(fp))  // repeat until reached end of file

{
	
    int index;	// index into the buffer
    numread = fread(buffer, sizeof(unsigned char), 64, fp);
    if (numread == 0)
        break;  // no data left to read
    for (index = 0; index < numread; ++index)
    {
        // process each byte of the buffer
        char b = buffer[index];	// b contains the value of the current byte
        printf("byte %d: %c", index, b);
		fprintf(pFile,"%c",b);
    }
	fclose (pFile);
	ret = remove(filename);

   if(ret == 0) 
   {
      printf("\n File deleted successfully");
   }
   else 
   {
      printf("\n Error: unable to delete the file");
   }
}





我的尝试:





What I have tried:

unsigned char buffer[64];	// buffer may be any size
FILE *fp = fopen(image.pgm,rb);
FILE *pFile;
char *filename="text";
int ret;
int numread;

pFile=fopen(filename,"wb");

while (!feof(fp))  // repeat until reached end of file
{
    int index;	// index into the buffer
    numread = fread(buffer, sizeof(unsigned char), 64, fp);
    if (numread == 0)
        break;  // no data left to read
    for (index = 0; index < numread; ++index)
    {
        // process each byte of the buffer
        char b = buffer[index];	// b contains the value of the current byte
        printf("byte %d: %c", index, b);
        fprintf(pFile,"%c";,b);
    }
	fclose (pFile);
	ret = remove(filename);

   if(ret == 0) 
   {
      printf("\n File deleted successfully");
   }
   else 
   {
      printf("\n Error: unable to delete the file");
   }

推荐答案

您正在尝试删除刚刚创建的文件(为什么?)。



如果失败,文件可能尚未创建(不存在)。

但你不知道这是因为你没有检查是否打开它是成功的( pFile 不是 NULL )。



所以你应该先检查一下(并检查输入文件是否已打开):

You are trying to delete a file that has just been created (why?).

If that fails, the file has probably not been created (does not exist).
But you don't know that because you are not checking if opening it was successful (pFile not NULL).

So you should check that first (and check also if the input file has been opened):
#include <errno.h> /* for perror() and/or errno */

FILE *fp = fopen(image.pgm,rb);
if (NULL == fp)
{
    perror("Failed to open input file");
    return;
}
char *filename = "text";
FILE pFile = fopen(filename,"wb");
if (NULL == pFile)
{
    perror("Failed to open output file");
    fclose(fp);
    return;
}
/* ... */
if (remove(filename))
{
    perror("Failed to delete output file");
    return;
}
</errno.h>





失败的可能原因是访问权限不足(你没有指定路径,以便在当前工作目录中创建文件。)



A possible reason for failure are insufficient access rights (you did not specify a path so that the file will be created in the current working directory).


首先找出系统没有删除它的原因。 remove 如果有问题则返回-1,并使用错误代码设置 errno ,所以先看一下: strerror [ ^ ]可以帮助您将其转换为可读字符串。



当你知道为什么时,问题是应该是相当明显的。
Start by finding out why the system didn't remove it. remove returns -1 if it has a problem, and sets errno with the error code, so start by looking at that: strerror[^] may help you by converting it to a readable string.

When you know why, it should be fairly obvious what the problem is.


这篇关于文件未在C中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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