fp = fopen("TENLINES.TXT","w"); /* open for writing */
strcpy(stuff,"This is an example line.");
for (index = 1; index <= 10; index++)
fprintf(fp,"%s Line number %d\n", stuff, index);
fclose(fp); /* close the file before ending program */
FILE * fopen(char * filename, char * mode)
The mode value in the above example is set to 'r', indicating that we want to read from the file. Other possible values are:
/* Program to display the contents of a file on screen */
fp = fopen("prog.c","r");
c = getc(fp) ; //reading
while (c!= EOF)
c = getc(fp);
The command to delete a file is:
remove (char * szFileName);
Writing to files can be done in various ways:
- putc() - like fputc()
- fputc() - int fputc (int character, FILE * stream); - write a character to a file
- fputs() - int fputs (const char * string , FILE * stream); - write a string to a file
- int fprintf(
FILE *stream, const char *format, ... )
The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like |printf()| as far as the format goes. The return value of fprintf() is the number of characters outputted, or a negative number if an error occurs.
just like reading from files:
char name = "Mary";
fprintf( out, "Hello %s\n", name );The fputs() function writes an array of characters pointed to by str to
the given output stream. The return value is non-negative on success,
and EOF on failure.
(Note: All functions have 1 argument as file pointer, just note the position of file pointer argument in them)
- getc(FILE * stream) - like fgetc()
- fgetc(FILE * stream) - int fgetc (FILE * stream); - write a character to a file
- fgets() - char * fgets (char * string , int num , FILE * stream); - write a string to a file
- int fscanf() - int fscanf ( FILE * stream , const char * format [ , argument , ...] ); - works like scanf() except that it reads from a file instead of STDIN
The getc() function returns the next character from stream, or EOF if the end of file is reached. getc() is identical to |fgetc()|.
The function fgets() reads up to num - 1 characters from the given file stream and dumps them into str. fgets() will stop when it reaches the end of a line, in which case str will be terminated with a newline. If fgets() reaches num - 1 characters or encounters the EOF, str will be null-terminated. fgets() returns str on success, and NULL on an error.
The function fscanf() reads data from the given file stream in a manner exactly like |scanf()|. The return value of fscanf() is the number of variables that are actually assigned values, or EOF if no assignments could be made.
Reading and writing in binary format
int fread( void *buffer, size_t size, size_t num, FILE *stream );
The function fread() reads num number of objects (where each object is size bytes) and places them into the array pointed to by buffer.
The data comes from the given input stream. The return value of the
function is the number of things read...use |feof()| or |ferror()| to
figure out if an error occurs.
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );Description:The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written.
Returns EOF on failure:
fscanf() , fclose(); fputs() ,
Returns 0 on success
Returns NULL on failure:
fopen(), freopen() ,
Returns non-0 on success
Returns negative no. of failure: