Showing posts with label String Input/Output in file. Show all posts
Showing posts with label String Input/Output in file. Show all posts

Friday 30 March 2012

String Input/Output in file


String Input/Output in file

fputs()

This function writes a string to the file.

Synatx:

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

Here, First argument is the string that needs to be written in the file and second argument is the pointer to structure FILE.
Ex,
FILE *fp;
fp = fopen(“str.txt”, “w”);
char *s=’Radha’;           
fputs(s,fp);

fgets()

This function reads a string from file.
Synatx:

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

This function requires three arguments , first is the address where string will be stored, the second is the maximum length of string and third is the pointer to FILE structure.

It reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string. A null character is appended to the end of the string.
On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged.
Ex,
FILE *fp;
fp = fopen(“str.txt”, “r”);
char *s;               
fgets(s,50,fp);

Posted By : Ruchita Pandya