-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFILE_IO.c
More file actions
83 lines (70 loc) · 2.05 KB
/
FILE_IO.c
File metadata and controls
83 lines (70 loc) · 2.05 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
FILE - container in a storage device(hardware) to store data.
RAM is volatile
Contents are lost when program terminates
Files are used to persist the data
Operation on Files:
Create a File
Open a file
Close a file
Read from a File
Write in a File
Types of Files:
1)Text Files:
textual data(.txt,.c,.py,.java etc...)
2)Binary Files:
binary data(.exe,.mp3,.jpg etc...)
File Pointer:
FILE is a (hidden) structure that needs to be created for opening a file.
A FILE ptr that points to this structure & is used to access the file.
FILE *fptr;
Opening a File:
fptr = fopen("filename",mode); [Mode = Read("r"),Write("w") etc...]
fptr = fopen("Test.txt", "r");
File Opening Modes:
"r" - open to read
"rb" - open to read in binary
"w" - open to write
"wb" - open to write in binary
"a" - open to append
Read a char:
fgetc(fptr);
OR
fscanf(fptr, "%c", &ch);
Write a char:
fputc('A',fptr);
OR
fprintf(fptr, "%c", 'A');
End Of File(EOF):
fgetc returns EOF to show that the file has ended.
ch = fgetc(fptr);
while (ch != EOF){
printf("%c",ch);
}
Closing a File:
fclose(fptr);
*/
#include <stdio.h>
int main()
{
// File Pointer
FILE *fptr;
// Opening a File
// fptr = fopen("filename",mode); [Mode = Read("r"),Write("w") etc...]
fptr = fopen("Test.txt", "r");
char ch;
// Reading from a file
fscanf(fptr, "%c", &ch);
// Writing from a file
fprintf(fptr, "%c", 'A');
// Output
printf("Character = %c\n", ch);
// Closing a File
fclose(fptr);
//End of File
ch = fgetc(fptr);
while(ch!=EOF){
printf("%c",ch);
}
return 0;
}