整理了一份C語言的文件讀寫件操作代碼,測試時打開相應的注釋即可。
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 /*
7 * EOF: 符號常量,其值為-1.
8 * fgets() 最多讀取int-1個字符,遇換行或EOF即返回.
9 * fputs() 寫文件時,忽略字符串截止符'\0'.
10 * fread()和fwrite() 數據塊讀寫,多用於結構體數組(順序存儲的結構體).
11 *
12 * 函數原型:
13 * 讀:
14 * int fgetc(FILE *stream);
15 * char *fgets(char *s, int size, FILE *stream);
16 * size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
17 * int fscanf(FILE *stream, const char *format, ...);
18 * 寫:
19 * int fputc(int c, FILE *stream);
20 * int fputs(const char *s, FILE *stream);
21 * size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
22 * int fprintf(FILE *stream, const char *format, ...);
23 * 其他:
24 * void rewind(FILE *stream); 將文件內部的位置指針重新指向一個流(數據流/文件)的開頭.
25 *
26 * 打開方式:
27 * r(read): 讀
28 * w(write): 寫
29 * a(append): 追加
30 * +: 讀和寫
31 * t(text): 文本文件,可省略不寫
32 * b(banary): 二進制文件
33 */
34
35 FILE *fp = NULL;
36
37 // READ
38 void GetCharFromFile(FILE *fp)
39 {
40 int ch = 0;
41
42 while ((ch=fgetc(fp)) != EOF) //失敗時返回 EOF
43 {
44 printf("fget: [%c]\n", ch);
45 }
46 }
47
48 void GetLineFromFile(FILE *fp)
49 {
50 char line[1024] = "";
51
52 while (fgets(line, sizeof(line), fp) != NULL) //失敗時返回 NULL
53 {
54 printf("fgets: [%s]\n", line);
55 }
56 }
57
58 void GetBlockFromFile(FILE *fp)
59 {
60 int i = 0;
61 int block[10] = {0};
62 int count = 1;
63
64 while (fread(block, sizeof(block), count, fp) == count) //失敗時返回值 != count
65 {
66 printf("fread: ");
67 for (i=0; i<10; i++)
68 {
69 printf("%d ", block[i]);
70 }
71 printf("\n");
72 }
73 }
74
75 void ReadFormatToFile(FILE *fp)
76 {
77 char file[32] = "";
78 char func[32] = "";
79 char date[32] = "";
80 int line = 0;
81
82 fscanf(fp, "%s %s %d %[^\n]", file, func, &line, date); //返回讀取元素個數,eg:4
83 printf("file: %s\n", file);
84 printf("func: %s\n", func);
85 printf("line: %d\n", line);
86 printf("date: %s\n", date);
87 }
88
89 // WRITE
90 int WriteCharToFile(FILE *fp, char ch)
91 {
92 int ret = 0;
93
94 ret = fputc(ch,fp); //失敗時返回EOF
95 return ret!=EOF?0:-1;
96 }
97
98 int WriteStrToFile(FILE *fp, char *str)
99 {
100 int ret = 0;
101
102 ret = fputs(str, fp);
103 return ret!=EOF?0:-1; //失敗時返回EOF
104 }
105
106 int WriteBlockToFile(FILE *fp, const void *block, int size, int count)
107 {
108 int ret = 0;
109
110 ret = fwrite(block, size, count, fp); //失敗時返回值 != count
111 return ret!=count?-1:0;
112 }
113
114 int WriteFormatToFile(FILE *fp)
115 {
116 int ret = 0;
117
118 ret = fprintf(fp, "%s %s %d %s\n", __FILE__, __func__, __LINE__, __DATE__);
119 return ret<0?-1:0; //失敗時返回一個負值
120 }
121
122 // MAIN
123 int main(int argc, char **argv)
124 {
125 char ch = 'r';
126 char *str = "Hello World.\n";
127 int block[10] = {1,2,3,4,5,6,7,8,9,0};
128 char *filePath = "./ll";
129
130 fp = fopen(filePath, "w+"); //不關心文件存在與否,每次重寫文件,並可讀
131 if (NULL == fp)
132 {
133 perror("fopen");
134 return -1;
135 }
136
137 // WRITE
138 // printf("WriteCharToFile: %s\n", WriteCharToFile(fp, ch)?"Fail":"Success");
139 printf("WriteStrToFile: %s\n", WriteStrToFile(fp, str)?"Fail":"Success");
140 // printf("WriteBlockToFile: %s\n", WriteBlockToFile(fp, block, sizeof(block), 1)?"Fail":"Success");
141 // printf("WriteFormatToFile: %s\n", WriteFormatToFile(fp)?"Fail":"Success");
142
143 rewind(fp);
144 // READ
145 // GetCharFromFile(fp);
146 GetLineFromFile(fp);
147 // GetBlockFromFile(fp);
148 // ReadFormatToFile(fp);
149
150 fclose(fp);
151 return 0;
152 }