本文介绍了文件未在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");
   }

推荐答案

#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).



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

08-27 08:46
查看更多