Files
a Open a text file and append text to the end of the file.
ab Open a binary file and append data to the end of the file.
fclose()
The fclose() function is used for closing opened files. The only argument it accepts is the file pointer. If
a program terminates, it automatically closes all opened files.
fclose("filename")
Q. Write the use of fprintf and fscanf functions in C language.
The fprintf and fscanf functions are identical to printf and scanf functions except that they work on
files. The first argument of theses functions is a file pointer which specifies the file to be used.
The general form of fprintf is
fprintf(fp,”control string”, list);
Where fp id is a file pointer associated with a file that has been opened for writing. The control string
is file output specifications list may include variable, constant and string.
fprintf(f1,%s%d%f”,name,age,7.5);
Here name is an array variable of type char and age is an int variable
The general format of fscanf is
fscanf(fp,”controlstring”,list);
This statement would cause the reading of items in the control string.
Example:
fscanf(f2,”5s%d”,item,&quantity”);
Q. Write short notes on i) fgets ii) fgetc() iii) fputs and iv) fputc().
fputs is a function in C programming language that writes an array of characters to a given file
stream. fputs stands for file put string. It is included in the C standard library header file stdio.h.
The function fputs terminates after reaching terminating null character ('\0'). The null character is not
copied to the stream. The prototype of the function is as follows:
int fputs ( const char * str, FILE * stream );
The stream argument specifies the stream to which the string will be written. stdout is commonly used
here, for writing to the standard output. Otherwise, a FILE * value returned by the fopen() function is
used.
The following example is 'hello world' program using fputs:
#include <stdio.h>
int main() {
const char *buffer = "Hello world!";
fputs (buffer, stdout);
return 0;
}
fgets is a function in C programming language that reads a limited number of characters from a given
file stream source into an array of characters. fgets stands for file get string. It is included in the C
standard library header file stdio.h. The prototype of the function is as follows:
char* fgets(char *string, int length, FILE * stream);
The function terminates reading either after a new-line character is found or end-of-file is reached, or
after (length - 1) characters have been read. If a new-line was reached it is included in the string as
the last character before the null character.
The stream argument specifies the stream from which the string be read. stdin is commonly used
here, for reading from the standard input.
The following code reads characters from the console input and prints them out 20 in a line with the
puts function until an EOF occurs.
#include <stdio.h>
#define MAX_LEN 20
int main(void)
{
char str_buf[MAX_LEN + 1]; // One extra byte needed
// for the null character
while(fgets(str_buf, MAX_LEN + 1, stdin) != NULL)
puts(str_buf);
return 0;
}
fputc is a function in C programming language that writes a character specified by the
argument char to the specified stream and advances the position indicator for the stream.
If there are no errors, the same character that has been written is returned. If an error occurs, EOF is
returned and the error indicator is set.
The syntax of fputc() function is :
int fputc(int char, FILE *stream)
where
char -- This is the character to be written. This is passed as its int promotion.
stream -- This is the pointer to a FILE object that identifies the stream where the character is to be
written.
Example
#include <stdio.h>
int main ()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "w+");
for( ch = 33 ; ch <= 100; ch++ )
{
fputc(ch, fp);
}
fclose(fp);
return(0);
}
If in text file file.txt has the content : we are in 2012
Then the OUTPUT is : we are in 2012
Fgetc is a function in C programming language that gets the next character from the specified
stream and advances the position indicator for the stream.
This function returns the character read as an unsigned char cast to an int or EOF on end of file or
error.
The syntax of fputc() function is :
int fgetc(FILE *stream)
where
stream -- This is the pointer to a FILE object that identifies the stream on which the operation is to
be performed.
Example
#include <stdio.h>
int main ()
{
FILE *fp;
int c;
int n = 0;
fp = fopen("file.txt","r");
if(fp == NULL)
{
perror("Error in opening file");
return(-1);
}
do
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}while(1);
fclose(fp);
return(0);
}