整理了一份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 }