-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfileoperations.c
45 lines (44 loc) · 1.1 KB
/
fileoperations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Implement a program to create a file and perform the following
// i. Write data to the file
// ii. Read the data in a given file & display the file content on console
// iii. Append new data and display on console do the following using pointers
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp;
char c;
fp = fopen("sample.txt", "w");
if (fp == NULL)
{
printf("file cannot be created!!");
exit(0);
}
printf("Enter the data (ctrl+d to stop) :\n");
while ((c = getchar()) != EOF)
{
putc(c, fp);
}
fclose(fp);
printf("\nEntered data: \n");
fp = fopen("sample.txt", "r");
while ((c = getc(fp)) != EOF)
{
putchar(c);
}
fclose(fp);
fp = fopen("sample.txt", "a");
printf("\nEnter the data to be added (ctrl+d to stop) :\n");
while ((c = getchar()) != EOF)
{
putc(c, fp);
}
fclose(fp);
printf("\nAppended data: \n");
fp = fopen("sample.txt", "r");
while ((c = getc(fp)) != EOF)
{
putchar(c);
}
fclose(fp);
}