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
This indicator is generally set by a previous operation on the stream that failed, and is cleared by a call to clearerr, rewindor 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
|
|
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
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
|
|
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 |