
Text File Input Output(I/O)
For manipulating files, we have to learn about file I/O which means how to write data and read data into and from the file. For reading and writing file we use the inbuilt functions fprintf() and fscand(). These two funstions are the file version of printf() and scanf(). The first argument is a pointer to file which makes the only difference between both.
Formatted I/O Functions:
fprintf()—writing in file
Example:
In this program, it reads a number and stores in file. After compilation by user and running it, user will get a text file sample.txt created in suppose C:\TURBOC3\path of a computer. When a user will open that particular file, user will get an integer which was entered.
Same is the case with fscanf() to read data from file.
void main()
{
int n;
FILE *fptr;
fptr=fopen("C:\TURBOC3\sample.txt","w");
if(fptr==NULL){
printf("Cannot Open File!");
exit(1);
}
printf("Enter a Number: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
}
fscanf()—Reading from file
In this program, it will read the integer which is preaent in sample.txt file and will print it on the screen.
These are the two formatted I/O functions which are used in file processing in C programming.
void main()
{
int n;
FILE *fptr;
if ((fptr=fopen("C:\TURBOC3\sample.txt","r"))==NULL){
printf("Cannot Open File ");
exit(1);
}
fscanf(fptr,"%d",&n);
printf("Value in file is=%d",n);
fclose(fptr);
}
Unformatted I/O Functions:
There are soo many unformatted I/O functions like fgetc() ,fputc(), etc... These two are used for reading and writing data into file.
fputc()/fputs()
fputc() or fputs() writes the character value of the argument. On success, it returns the written character otherwise End of File (EOF).
int fputc(int c,FILE*fp);
For fputs(), it writes the string s to the output stream. It returns non-negative value on success otherwise EOF.
int fputs(const chars, FILEfp);
Example:
void main() {
FILE *fptr;
fptr = fopen("C:\TURBOC3\sample.txt", "w+");
fputs("Printing Using fputs", fptr);
fclose(fptr);
}
After the execution of the above program, it will create a new sample.txt in C:\TURBOC3\ path and will write the string " Printing Using fputs" to the file.
fgetc()/fgets()
fgetc() function reads a character from the input file refered by fp whose return value is character read or EOF in case of error.
int fgetc(FILE*fp);
fgets() reads upto n-1 characters from the input file refered by fp. It copies read string into buffer buf and appends a null character to terminate the string.
char *fgets(char *buf,int n, FILE *fp);
Example:
When above written program will get executed, it will read the file created in the previous program and will produce the result as:
Printing Using fputs

void main()
{
FILE *fptr;
char buff[255];
fptr = fopen("C:\TURBOC3\sample.txt", "r");
fgets(buff, 255, fptr);
printf("%s\n", buff );
fclose(fptr);
}
Thank you.
I hope you guys liked my post.
Keep Supporting.
STAY TUNED FOR NEXT TOPIC
UPVOTE COMMENT RESTEEM
IF YOU LIKED MY POST


Stay Home, Stay Safe
@peerzadazeeshan
I hope you guys liked my post.
Keep Supporting.
STAY TUNED FOR NEXT TOPIC
| UPVOTE | COMMENT | RESTEEM |
|---|---|---|
| IF YOU | LIKED | MY POST |

