File Handling in C

File Pointer : There is a declaration of FILE structure inside stdio.h , we can declare a pointer like this
FILE *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.
ModeDetailsRemarks
rReadFile opned & content can be collected, no updating , adding is allowed
wwriteFile created if not available, content are overwritten
aappendFile created if not available, content written at the end of the file
r+ReadFile opened for reading and writing
w+writeFile opened for reading and writing, a new file created
a+appendFile 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 str of 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;
}
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer