File Handling in C Programming - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

File Handling in C Programming

Share This
Till now whatever programs we have written, the data is not being saved anywhere in the system. Hence, in every run it works on new data.

If you want to store data permanently in your computing device, you have to work with file system. Files can be divided into two categories: text file and binary file. In a program to work with file, we have to follow some steps as described below.
  • Open a file using fopen function
  • Read, write or edit content using input and output functions
  • Close the file using fclose function
The signature of fopen function is shown below.

file_pointer fopen ("file_name", "mode");

According to the requirement, a text file is opened in different modes as shown below.

"r" for reading. If the file does not exist, fopen() returns NULL
"w" for writing If the file exists, its contents are overwritten. If the file does not exist, it will be created
"a" for appending Data is added to the end of the file. If the file does not exist, it will be created.
"r+" for reading and writing If the file does not exist, fopen() returns NULL
"w+" for reading and writing If the file exists, its contents are overwritten. If the file does not exist, it will be created
"a+" for reading and appending If the file does not exist, it will be created
To work with binary files, instead of r, w, a, r+, w+, a+, you have to use rb, wb, ab, r+b, w+b, and a+b respectively.


Writing in a file


In the following program, we have shown how to write in a file.


#include <stdio.h> int main() { int n; FILE *fptr; fptr=fopen("program.txt","w"); if(fptr==NULL){ printf("Error!"); exit(1); } printf("Enter n: "); scanf("%d",&n); fprintf(fptr,"%d",n); fclose(fptr); return 0; }

Reading from a file


In the following program, we have shown how to read from a file.

#include <stdio.h> int main() { int n; FILE *fptr; if ((fptr=fopen("program.txt","r"))==NULL){ printf("Error! opening file"); exit(1); } fscanf(fptr,"%d",&n); printf("Value of n=%d",n); fclose(fptr); return 0; }

Appending a text in a file


In the following program, we have shown how to append a string to a file.

#include <stdio.h> int main(void) { FILE *fp; char *str; str = (char *)malloc(100); fp = fopen("abc.txt", "a"); if(fp == NULL) { printf("Unable to open file!"); exit(1); } printf("Enter a string:"); scanf("%s", str); fputs("\n\n", fp); fputs(str, fp); fclose(fp); printf("\n\n"); return 0; }

A Program to Copy Content of a File to Another


In the following program, we have shown how the content of a file is copied into another file.

#include <stdio.h> #include <stdlib.h> int main() { FILE *fpsrc; FILE *fptar; char filename[32]; char c; printf("\nEnter The Source File Name: "); scanf("%s", filename); fpsrc = fopen(filename, "r"); if (fpsrc == NULL) { printf("\nThe Source File you specified was not found\n"); exit(0); } printf("\nEnter The Target File Name: \t"); scanf("%s", filename); fptar = fopen(filename, "w"); while ((c = fgetc(fpsrc)) != EOF) fputc(c, fptar); fclose(fptar); fclose(fpsrc); }

A Program to count words in a file.



#include <stdio.h> int isSpecial(char c); int main() { FILE *fp; char c, pc; int word_count = 0; fp = fopen ("source.txt", "r"); c = fgetc(fp); while(c != EOF) { if (isSpecial(c) && !isSpecial(pc)) word_count++; pc = c; c = fgetc(fp); } if (!isSpecial(pc)) word_count++; printf("%d", word_count); return 0; } int isSpecial(char c) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) return 0; else return 1; }

A program to erase comment lines from the source code.


#include <stdio.h> #include <ctype.h> int main() { FILE * fpread, *fpwrite; int flag = 0, key = 0; char a, b, name1[10], name2[10]; printf(">> source ? \n>> "); scanf("%s", name1); printf(">> detination ? \n>> "); scanf("%s", name2); fpwrite = fopen(name2, "w"); if ((fpread = fopen(name1, "r")) == NULL) printf("\n>> the requested file could not be found \n"); else { printf("\n\n source file \n\n"); do { a = getc(fpread); putchar(a); if (a == '/'/) { b = getc(fpread); putchar(b); if (b == '/'/ || b == '*') { flag = 1; if (b == '*') key = 1; } else { putc(a, fpwrite); putc(b, fpwrite); } } if (flag == 1) { if (key == 0) while (a != '\n') { a = getc(fpread); putchar(a); } if (key == 1) { while (key) { a = getc(fpread); putchar(a); if (a == '*') { b = getc(fpread); putchar(b); if (b == '/'/) key = 0; } } } putc('\n', fpwrite); flag = 0; } else putc(a, fpwrite); } while (!feof(fpread)); printf("\a\a"); fclose(fpwrite); fclose(fpread); printf("\n\n destination file \n\n"); fpread = fopen(name2, "r"); while (!feof(fpread)) putchar(a = getc(fpread)); } }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.