0% found this document useful (0 votes)
41 views7 pages

Files Practice Questions 2

The document contains 6 C programs related to file handling operations: 1. Creating a file and storing user input data like name, age, salary. 2. Reading data from an existing file by taking user input for file name. 3. Deleting a specific user-defined line from a text file. 4. Replacing a specific user-defined line in a text file with new input text. 5. Counting the number of lines in a text file. 6. Appending the contents of one file to the end of another file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Files Practice Questions 2

The document contains 6 C programs related to file handling operations: 1. Creating a file and storing user input data like name, age, salary. 2. Reading data from an existing file by taking user input for file name. 3. Deleting a specific user-defined line from a text file. 4. Replacing a specific user-defined line in a text file with new input text. 5. Counting the number of lines in a text file. 6. Appending the contents of one file to the end of another file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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. }

2. 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. }

3. 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. }

4. 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. }

5. 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. }

6. C Program to Append the Content of File at the end of Another

1. #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. }

You might also like