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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
//fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict fp); //fwrite返回成功写入的项目数,正常情况下,它与nmemb相等,如果有写入错误,返回值将小于nmemb #include <stdio.h> int main(int argc, char const *argv[]) { //将一块256字节大小的数据从缓冲区写入到文件 char buffer[256]; fwrite(buffer, 256, 1, fp); //将earnings数组中的数据写入文件,数据分成10块,每块都是double大小 double earnings[10]; fwrite(earnings, sizeof(double), 10, fp); //fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict fp); //fread返回成功读入的项目数,正常情况下,它与nmemb相等,如果有读取错误,返回值将小于nmemb double earnings[10]; fread(earnings, sizeof(double), 10, fp); return 0; } // 利用fread() fwrite() setvbuf()来读取指定文件(可以多个)来追加目标文件 #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFSIZE 1024 #define SLEN 81 void append(FILE *source, FILE *dest); int main(void) { // int setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size); // setvbuf()函数建立一个供标准I/O函数使用的替代缓冲区(不使用fopen打开的默认缓冲区) // 当需要处理数据库、视频、音频、图像等大量需要爆发式磁盘到内存的I/O情况下可以考虑使用 // 如果buf不是NULL,则必须创建这个缓冲区,例如一个1024字节的数组,然后传递该数组的地址 // 如果buf为NULL,函数会自动malloc()一个缓冲区 // mode取值范围如下: // _IOFBF:完全缓冲(缓冲区满的时候刷新) // _IOLBF:行缓冲(缓冲区满或一个新行写入的时候刷新) // _IONBF:无缓冲 // size表示缓冲区的长度(必须大于0) // 返回值 成功:0 失败:非0 FILE *fa, *fs; int files = 0; char file_app[SLEN]; char file_src[SLEN]; puts("Enter name of destination file:"); gets(file_app); if ((fa = fopen(file_app, "a")) == NULL) { fprintf(stderr, "Can't open %s\n", file_app); exit(2); } if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0) { fputs("Can't create output buffer\n", stderr); exit(3); } puts("Enter name of first source file(empty line to quit):"); while(gets(file_src) && file_src[0] != '\0') { if (strcmp(file_src, file_app) == 0) { fputs("Can't append file to itself\n", stderr); } else if((fs = fopen(file_src, "r")) == NULL) { fprintf(stderr, "Can't open%s\n", file_src); } else { if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0) { fputs("Can't create input buffer\n", stderr); fclose(fs); continue; } append(fs, fa); //当标准输入函数EOF时,通常表示已经达到了文件结尾,但也有可能表示发生了错误 //使用feof() ferror()可以区分这两种可能性。 //如果最近一次输入调用检测到文件结尾,feof()返回一个非0值,否则返回0值 //如果发生了读写错误,ferror()返回非0值,否则返回0值 if (ferror(fs) != 0) { fprintf(stderr, "Error in reading file%s\n", file_src); files--; } if (ferror(fa) != 0) { fprintf(stderr, "Error in writing file%s\n", file_app); files--; } fclose(fs); files++; printf("File%s appended\n", file_src); puts("Next file(empty line to quit):"); } } printf("Done %d files appended\n", files); fclose(fa); return 0; } void append(FILE *source, FILE *dest) { size_t bytes; static char temp[BUFSIZE]; while((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0) { fwrite(temp, sizeof(char), bytes, dest); } } // 随机读 #include <stdio.h> #include <stdlib.h> #define ARSIZE 1000 int main(int argc, char const *argv[]) { double numbers[ARSIZE]; double value; const char * file = "numbers.dat"; int i; long pos; FILE * iofile; for (i = 0; i < ARSIZE; ++i) { numbers[i] = 100.0 * i + 1.0 / (i+1); } if ((iofile = fopen(file, "wb")) == NULL) { fprintf(stderr, "Could not open %s for output\n", file); exit(1); } fwrite(numbers, sizeof(double), ARSIZE, iofile); fclose(iofile); if ((iofile = fopen(file, "rb")) == NULL) { fprintf(stderr, "Could not open %s for random access\n", file); exit(1); } printf("Enter an index in the range 0-%d\n", ARSIZE - 1); scanf("%d", &i); while(i >= 0 && i < ARSIZE) { pos = (long)i * sizeof(double); fseek(iofile, pos, SEEK_SET); fread(&value, sizeof(double), 1, iofile); printf("The value there is %f \n", value); printf("Next index (out of range to quit):\n"); scanf("%d", &i); } fclose(iofile); puts("Done"); return 0; } |