Click here to Skip to main content
15,942,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to delete the contains of a file ...but not deletetd
C++
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:

C++
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");
   }
Posted
Updated 24-Oct-16 5:07am
v2

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):
C++
#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).
 
Share this answer
 
Comments
yagami_md 24-Oct-16 11:28am    
I tried that program show :
No such file or directory
yagami_md 24-Oct-16 11:36am    
Failed to delete filename : permission denied
Jochen Arndt 24-Oct-16 12:06pm    
The message is quite clear:
You do not have sufficient rights (privileges) to delete the file.

The problem is that you did not specify a path and only you might know which path is actually used. You can use getcwd() and print it out to know where your file is stored and check the permissions.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900