rewind(), ferror, erasing file

Description

The C library function void rewind(FILE *stream) sets the file position to the beginning of the file of the given stream.

Declaration

Following is the declaration for rewind() function.
void rewind(FILE *stream)

Parameters

  • stream − This is the pointer to a FILE object that identifies the stream.

Return Value

This function does not return any value.

Example

The following example shows the usage of rewind() function.
#include <stdio.h>

int main()
{
   char str[] = "This is tutorialspoint.com";
   FILE *fp;
   int ch;

   /* First let's write some content in the file */
   fp = fopen( "file.txt" , "w" );
   fwrite(str , 1 , sizeof(str) , fp );
   fclose(fp);

   fp = fopen( "file.txt" , "r" );
   while(1)
   {
      ch = fgetc(fp);
      if( feof(fp) )
      {
         break ;
      }
      printf("%c", ch);
   }
   rewind(fp);
   printf("\n");
   while(1)
   {
      ch = fgetc(fp);
      if( feof(fp) )
      {
         break ;
      }
      printf("%c", ch);
     
   }
   fclose(fp);

   return(0);
}
Let us assume we have a text file file.txt that have the following content −
This is tutorialspoint.com
Now let us compile and run the above program to produce the following result −
This is tutorialspoint.com
This is tutorialspoint.comint 

Ferror
ferror ( FILE * stream );
Check error indicator
Checks if the error indicator associated with stream is set, returning a value different from zero if it is.

This indicator is generally set by a previous operation on the stream that failed, and is cleared by a call to clearerrrewindor freopen.

Parameters

stream
Pointer to a FILE object that identifies the stream.

Return Value

A non-zero value is returned in the case that the error indicator associated with the stream is set.
Otherwise, zero is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* ferror example: writing error */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile=fopen("myfile.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else {
    fputc ('x',pFile);
    if (ferror (pFile))
      printf ("Error Writing to myfile.txt\n");
    fclose (pFile);
  }
  return 0;
}


This program opens an existing file called myfile.txt in read-only mode but tries to write a character to it, generating an error that is detected by ferror.

Output:
Error Writing to myfile.txt

Remove


remove

int remove ( const char * filename );
Remove file
Deletes the file whose name is specified in filename.

This is an operation performed directly on a file identified by its filename; No streams are involved in the operation.

Proper file access shall be available.

Parameters

filename
C string containing the name of the file to be deleted.
Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).

Return value

If the file is successfully deleted, a zero value is returned.
On failure, a nonzero value is returned.
On most library implementations, the errno variable is also set to a system-specific error code on failure.

Example

1
2
3
4
5
6
7
8
9
10
11
/* remove example: remove myfile.txt */
#include <stdio.h>

int main ()
{
  if( remove( "myfile.txt" ) != 0 )
    perror( "Error deleting file" );
  else
    puts( "File successfully deleted" );
  return 0;
}


If the file myfile.txt exists before the execution and the program has write access to it, the file would be deleted and this message would be written to stdout:
File successfully deleted

Otherwise, a message similar to this would be written to stderr:
Error deleting file: No such file or directory