Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

C Program to Create a File & Store

Information
1. #include <stdio.h>
2.
3. void main()
4. {
5. FILE *fptr;
6. char name[20];
7. int age;
8. float salary;
9.
10. /* open for writing */
11. fptr = fopen("emp.rec", "w");
12.
13. if (fptr == NULL)
14. {
15. printf("File does not exists \n");
16. return;
17. }
18. printf("Enter the name \n");
19. scanf("%s", name);
20. fprintf(fptr, "Name = %s\n", name);
21. printf("Enter the age\n");
22. scanf("%d", &age);
23. fprintf(fptr, "Age = %d\n", age);
24. printf("Enter the salary\n");
25. scanf("%f", &salary);
26. fprintf(fptr, "Salary = %.2f\n", salary);
27. fclose(fptr);
28. }
Output:
Enter the name
raj
Enter the age
40
Enter the salary
4000000


C Program to Illustrate Reading of Data
from a File
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. void main()
5. {
6. FILE *fptr;
7. char filename[15];
8. char ch;
9.
10. printf("Enter the filename to be opened \n");
11. scanf("%s", filename);
12. /* open the file for reading */
13. fptr = fopen(filename, "r");
14. if (fptr == NULL)
15. {
16. printf("Cannot open file \n");
17. exit(0);
18. }
19. ch = fgetc(fptr);
20. while (ch != EOF)
21. {
22. printf ("%c", ch);
23. ch = fgetc(fptr);
24. }
25. fclose(fptr);
26. }









C Program Delete a specific Line from a
Text File
1. #include <stdio.h>
2.
3. int main()
4. {
5. FILE *fileptr1, *fileptr2;
6. char filename[40];
7. char ch;
8. int delete_line, temp = 1;
9.
10. printf("Enter file name: ");
11. scanf("%s", filename);
12. //open file in read mode
13. fileptr1 = fopen(filename, "r");
14. ch = getc(fileptr1);
15. ` while (ch != EOF)
16. {
17. printf("%c", ch);
18. ch = getc(fileptr1);
19. }
20. //rewind
21. rewind(fileptr1);
22. printf(" \n Enter line number of the line to be deleted:");
23. scanf("%d", &delete_line);
24. //open new file in write mode
25. fileptr2 = fopen("replica.c", "w");
26. ch = getc(fileptr1);
27. while (ch != EOF)
28. {
29. ch = getc(fileptr1);
30. if (ch == '\n')
31. temp++;
32. //except the line to be deleted
33. if (temp != delete_line)
34. {
35. //copy all lines in file replica.c
36. putc(ch, fileptr2);
37. }
38. }
39. fclose(fileptr1);
40. fclose(fileptr2);
41. remove(filename);
42. //rename the file replica.c to original name
43. rename("replica.c", filename);
44. printf("\n The contents of file after being modified are as follows:\n");
45. fileptr1 = fopen(filename, "r");
46. ch = getc(fileptr1);
47. while (ch != EOF)
48. {
49. printf("%c", ch);
50. ch = getc(fileptr1);
51. }
52. fclose(fileptr1);
53. return 0;
54. }

C Program to Replace a specified Line in
a Text File
1. #include <stdio.h>
2.
3. int main(void)
4. {
5. FILE *fileptr1, *fileptr2;
6. char filechar[40];
7. char c;
8. int delete_line, temp = 1;
9.
10. printf("Enter file name: ");
11. scanf("%s", filechar);
12. fileptr1 = fopen(filechar, "r");
13. c = getc(fileptr1);
14. //print the contents of file .
15. while (c != EOF)
16. {
17. printf("%c", c);
18. c = getc(fileptr1);
19. }
20. printf(" \n Enter line number to be deleted and replaced");
21. scanf("%d", &delete_line);
22. //take fileptr1 to start point.
23. rewind(fileptr1);
24. //open replica.c in write mode
25. fileptr2 = fopen("replica.c", "w");
26. c = getc(fileptr1);
27. while (c != EOF)
28. {
29. if (c == 'n')
30. {
31. temp++;
32. }
33. //till the line to be deleted comes,copy the content to other
34. if (temp != delete_line)
35. {
36. putc(c, fileptr2);
37. }
38. else
39. {
40. while ((c = getc(fileptr1)) != 'n')
41. {
42. }
43. //read and skip the line ask for new text
44. printf("Enter new text");
45. //flush the input stream
46. fflush(stdin);
47. putc('n', fileptr2);
48. //put 'n' in new file
49. while ((c = getchar()) != 'n')
50. putc(c, fileptr2);
51. //take the data from user and place it in new file
52. fputs("n", fileptr2);
53. temp++;
54. }
55. //continue this till EOF is encountered
56. c = getc(fileptr1);
57. }
58. fclose(fileptr1);
59. fclose(fileptr2);
60. remove(filechar);
61. rename("replica.c", filechar);
62. fileptr1 = fopen(filechar, "r");
63. //reads the character from file
64. c = getc(fileptr1);
65. //until last character of file is encountered
66. while (c != EOF)
67. {
68. printf("%c", c);
69. //all characters are printed
70. c = getc(fileptr1);
71. }
72. fclose(fileptr1);
73. return 0;
74. }
C Program to Find the Number of Lines in
a Text File
1. #include <stdio.h>
2.
3. int main()
4. {
5. FILE *fileptr;
6. int count_lines = 0;
7. char filechar[40], chr;
8.
9. printf("Enter file name: ");
10. scanf("%s", filechar);
11. fileptr = fopen(filechar, "r");
12. //extract character from file and store in chr
13. chr = getc(fileptr);
14. while (chr != EOF)
15. {
16. //Count whenever new line is encountered
17. if (chr == 'n')
18. {
19. count_lines = count_lines + 1;
20. }
21. //take next character from file.
22. chr = getc(fileptr);
23. }
24. fclose(fileptr); //close file.
25. printf("There are %d lines in %s in a file\n", count_lines, filechar);
26. return 0;
27. }
1.
C Program to Append the Content of File
at the end of Another
#include <stdio.h>
2. #include <stdlib.h>
3.
4. main()
5. {
6. FILE *fsring1, *fsring2, *ftemp;
7. char ch, file1[20], file2[20], file3[20];
8.
9. printf("Enter name of first file ");
10. gets(file1);
11. printf("Enter name of second file ");
12. gets(file2);
13. printf("Enter name to store merged file ");
14. gets(file3);
15. fsring1 = fopen(file1, "r");
16. fsring2 = fopen(file2, "r");
17. if (fsring1 == NULL || fsring2 == NULL)
18. {
19. perror("Error has occured");
20. printf("Press any key to exit...\n");
21. exit(EXIT_FAILURE);
22. }
23. ftemp = fopen(file3, "w");
24. if (ftemp == NULL)
25. {
26. perror("Error has occures");
27. printf("Press any key to exit...\n");
28. exit(EXIT_FAILURE);
29. }
30. while ((ch = fgetc(fsring1)) != EOF)
31. fputc(ch, ftemp);
32. while ((ch = fgetc(fsring2) ) != EOF)
33. fputc(ch, ftemp);
34. printf("Two files merged %s successfully.\n", file3);
35. fclose(fsring1);
36. fclose(fsring2);
37. fclose(ftemp);
38. return 0;
39. }
C Program that Merges Lines
Alternatively from 2 Files & Print Result
1. #include<stdio.h>
2. main()
3. {
4. char file1[10], file2[10];
5.
6. puts("enter the name of file 1"); /*getting the names of file to be concatenated*/
7. scanf("%s", file1);
8. puts("enter the name of file 2");
9. scanf("%s", file2);
10. FILE *fptr1, *fptr2, *fptr3;
11. fptr1=fopen(file1, "r"); /*opening the files in read only mode*/
12. fptr2=fopen(file2, "r");
13. fptr3=fopen("merge2.txt", "w+"); /*opening a new file in write,update mode*/
14. char str1[200];
15. char ch1, ch2;
16. int n = 0, w = 0;
17. while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
18. {
19. if (ch1 != EOF) /*getting lines in alternately from two files*/
20. {
21. ungetc(ch1, fptr1);
22. fgets(str1, 199, fptr1);
23. fputs(str1, fptr3);
24. if (str1[0] != 'n')
25. n++; /*counting no. of lines*/
26. }
27. if (ch2 != EOF)
28. {
29. ungetc(ch2, fptr2);
30. fgets(str1, 199, fptr2);
31. fputs(str1, fptr3);
32. if (str1[0] != 'n')
33. n++; /*counting no.of lines*/
34. }
35. }
36. rewind(fptr3);
37. while ((ch1 = fgetc(fptr3)) != EOF) /*countig no.of words*/
38. {
39. ungetc(ch1, fptr3);
40. fscanf(fptr3, "%s", str1);
41. if (str1[0] != ' ' || str1[0] != 'n')
42. w++;
43. }
44. fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n", n, w - 1);
45. /*appendig comments in the concatenated file*/
46. fclose(fptr1);
47. fclose(fptr2);
48. fclose(fptr3);
49. }
C program to read numbers from a file and write even, odd and prime numbers
in separate files
#include <stdio.h>
int main() {
FILE *fp1, *fp2, *fp3, *fp4;
int n, i, num, flag = 0;

/* open data.txt in read mode */
fp1 = fopen("data.txt", "w");
printf("Enter the value for n:");
scanf("%d", &n);
for (i = 0; i <= n; i++)
fprintf(fp1, "%d ", i);
fprintf(fp1, "\n");
fclose(fp1);

/* open files to write even, odd and prime nos separately */
fp1 = fopen("data.txt", "r");
fp2 = fopen("even.txt", "w");
fp3 = fopen("odd.txt", "w");
fp4 = fopen("prime.txt", "w");

fprintf(fp2, "Even Numbers:\n");
fprintf(fp3, "Odd Numbers:\n");
fprintf(fp4, "Prime Numbers:\n");

/* print even, odd and prime numbers in separate files */
while (!feof(fp1)) {
fscanf(fp1, "%d", &num);
if (num % 2 == 0) {
fprintf(fp2, "%d ", num);
} else {
if (num > 1) {
for (i = 2; i < num; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (!flag) {
fprintf(fp4, "%d ", num);
}
}
fprintf(fp3, "%d ", num);
flag = 0;
}
}
fprintf(fp2, "\n");
fprintf(fp3, "\n");
fprintf(fp4, "\n");

/* close all opened files */
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
return 0;
}

You might also like