-->
Showing posts with label about fopen. Show all posts
Showing posts with label about fopen. Show all posts

NEB32:three data file handling functions with examples.

Explain any three data file handling functions with examples.
ans:-
There are many data file functions which we use while programming. Following are some functions used to handle data stored in data file.

1) fopen(): It is used to open the file in particular mode. Its syntax is
                FILE *fopen(const char *filename, const char *mode); 
or 
FILE pointer= fopen("file name","mode");

Example
FILE *k;
k=fopen("data.txt","w");
This statement opens the file named data.txt in writing mode(w). Here ,writing mode means, we are going to store data in data file .

2)fclose(): It is used to close the opened file.As we have finished working with data file,it needs to be closed; so, at last we close it using fclose().

syntax
fclose(FILE name);
or fclose(FILE stream);

example
FILE *k;

k=fopen("data.txt","w");

..................

fclose(k);
Here we have closed data file using fclose() function.

3) fprintf(): It is used to write data/records in an opened data file i.e it writes data in an opened data file.

 syntax
int fprintf(FILE *stream,constant char *format);

example;
FILE *k;

k=fopen("data.txt","w");

fprintf(k,"%s %s  %s","hello","world", "of c");

this writes data (mentioned above) in an opened file data.txt.
4) fscan(): It is used to read data/records from opened data file. 
 syntax
int fscanf(FILE *stream,constant char *format,...);

example;
FILE *k;

k=fopen("data.txt","r");
char string[100];fscanf(k,"%s %s  %s",string,string,string);

this reads data (mentioned above) from opened file data.txt.