Explain different file I/O functions with example.

Answers

This answer is not selected as best answer. This answer may not be sufficient for exam.

Your limit has been exceed. We have implemented this system because, We got difficulty on managing our servers. Please donate some amount to remove this limit.

Quota: 0 / 30

Donate

C provides a number of functions that helps to perform basic file operations.

Following are the functions:

1. fopen()

It is used to create new file or open a existing file

Syntax:

ptr = fopen("fileopen","mode");

Example:

fopen("E:\\cprogram\\program.txt", "w");

It will create file program.txt if not exists in the path E:\\cprogram\\

2. fclose()

It is used to close the file after reading and writing.

fclose(fptr);

3. getc()

It is used to read the character from the file

Syntax

int getc(FILE *stream)

4. fprintf()

It is used to write a set of data values to files

Syntax:

int fprintf(FILE *stream, const char *format, ...)

Example:

fp = fopen("file.txt", "w+");
fprintf(fp, "Welcome to Hamro CSIT");

5. fscanf()

It is used to read a set of data values to files

Syntax

int fscanf(FILE *stream, const char *format, ...)

Example:

char str1[10], str2[10], str3[10];

FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("We are in 2012", fp);

fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);

6. fputs()

It is used to write a string to file

Syntax:

int fputs(const char *str, FILE *stream)

Example:

fputs("This is c programming.", fp);

7. fgets()

It is used to read a line

Syntax

char *fgets(char *str, int n, FILE *stream)

Example:

char str[60];
fgets (str, 60, fp)

 

If you found any type of error on the answer then please mention on the comment or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .