File Handling in C
File Handling in C
<plus2net_index> File Pointer : There is a declaration of FILE structure inside stdio.h, we can declare a pointer like thisFILE *fpt;
fopen()
: Opens file in different modes and return the pointer to its memory location.
fpt=fopen("data.csv","r");
fprintf()
Writting to file through file pointer
fprintf(fpt,"welcome to my file");
Different modes of file opening are given here.
Mode | Details | Remarks |
---|---|---|
r | Read | File opned & content can be collected, no updating , adding is allowed |
w | write | File created if not available, content are overwritten |
a | append | File created if not available, content written at the end of the file |
r+ | Read | File opened for reading and writing |
w+ | write | File opened for reading and writing, a new file created |
a+ | append | File opened for reading and appending |
getc()
: Get a single character from the file pointer
c=getc(fpt);
putchar()
: prints the single character to screen
putchar(c); putchar(c=getc(fpt));// getc reads from file pointer fpt
fgets()
: Getting string from the file pointer and place in the variable strof a fixed buffer size
fgets(str,99,fpt)
fputs()
: Writting string to file.
fputs(str,fpt);
EOF
: End of file is defined inside stdio.h. Reading a special character whose ASCII value is 26, signifies end of the file.
Reading a file and displaying content
#include <stdio.h> int main(void){ FILE *fpt; // file pointer char c; fpt=fopen("data.csv","r"); // file already exit for reading if(fpt==NULL) printf("Can't open file "); else { while(c!='\n'){ putchar(c=getc(fpt)); } fclose(fpt); } return 0; }
In the above code while loop will execute as long as the pointer not reached the first line break . Line breaks are identified by \n
while(c!='\n')
We can change this to read the complete file by checking EOF ( End of File )
while(c!=EOF){ putchar(c=getc(fpt)); }
Reading string from file
#include <stdio.h> int main(void){ FILE *fpt; char str[100]; fpt=fopen("data4.dat","r"); if(fpt==NULL) printf("Can't open file "); else { while(fgets(str,99,fpt) != NULL){ printf("%s",str); } } fclose(fpt); return 0; }
Writting to File
#include <stdio.h> int main(void){ FILE *fpt; // File pointer char c; fpt=fopen("data1.dat","w"); // Opening file in write mode fprintf(fpt,"welcome to my file"); fprintf(fpt,"\n Next Line "); fclose(fpt); return 0; }
Writting string to File
We can enter one line of code at a time and hit enter to complete the line. Each line is checked for its length by using strlen() and once we enter twice enter key the length of line will be equal to 0 and the while loop will terminate.So hit enter twice to end the entry of line of text to file.
#include <stdio.h> #include <string.h> int main(void){ FILE *fpt; char str[100]; fpt=fopen("data4.dat","w"); if(fpt==NULL) printf("Can't open file "); else { printf("Enter one line of text\n"); while(strlen(gets(str))>0){ fputs(str,fpt); fputs("\n",fpt); } } fclose(fpt); return 0; }
File Append
We can add text and end of the file by opening the file in appending mode.#include <stdio.h> int main(void){ FILE *fpt; char c; fpt=fopen("data1.dat","a"); fprintf(fpt,"\n my new line "); fprintf(fpt,"\n my new Next Line "); fclose(fpt); return 0; }
Writting to a file through structure
#include <stdio.h> struct my_class{ int id; char name[50]; int mark; float av; }; int main(void){ FILE *fpt; fpt=fopen("emp_data.dat","w"); struct my_class emp; if(fpt==NULL){ puts("Cant open file"); // exit(); } printf("\nEnter id:"); scanf("%d",&emp.id); fflush ( stdin ) ; printf("\nEnter Name:"); gets(emp.name); printf("\n mark:"); scanf("%d",&emp.mark); printf("\n Average :"); scanf("%f",&emp.av); printf("\n**************\n"); fprintf(fpt,"%d,%s,%d,%f \n",emp.id,emp.name,emp.mark,emp.av); printf("\n name:%s",emp.name); printf("\n mark:%d",emp.mark); printf("\n id:%f",emp.av); return 0; }
<fb:follow "https://www.facebook.com/joombig" width="550" layout="button_count" show_faces="true"><a "https://twitter.com/share" class="twitter-share-button" data-via="plus2net">
Basics of C printf : Sending output to screen scanf : Reading data from user or keyboard as input to C program comments : Using comments inside C program for loop in C program if else condition in C program switch statment to execute part of the program in C program getchar() function to read data from keyboard putchar() to read single data
Related Article
destination source:https://www.plus2net.com/c-tutorial/file-handling.php