编程环境Visual Studio 2017
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <memory.h> #include <ctype.h> // practice 1 int p13_1(void) { int ch; FILE *fp; unsigned long count = 0; char file_name[256]; printf("Enter the file name: "); scanf_s("%s", file_name, 256); if ((fp = fopen(file_name, "r")) == NULL) // fopen函数在VS2017中报告会报错,解决办法是在 项目 =》属性 =》c/c++ =》预处理器=》点击预处理器定义,编辑,加入_CRT_SECURE_NO_WARNINGS { // scanf_s之类的安全增强函数应该也适用(具体还没有试过),早知道就百度查一下了,之前使用的都是scanf_s printf("Can't not open %s\n", file_name); } while ((ch = getc(fp)) != EOF) { putc(ch, stdout); count++; } fclose(fp); printf("File %s has %lu characters\n", file_name, count); return 0; } // practice 2 void p13_2(void) { FILE *fp1; FILE *fp2; char *file1 = "data1.dat"; char *file2 = "data2.dat"; int ch = 0; if ((fp1 = fopen(file1, "rb")) == NULL) { printf("Can't open %s\n", file1); } if ((fp2 = fopen(file2, "wb")) == NULL) { printf("Can't open %s\n", file2); } while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2);; } fclose(fp1); fclose(fp2); return; } //practice 3 void p13_3(void) { char file_name[256]; char *tmp_file = "tmp.txt"; // 现在只能想到把读出来的数据保存到一个缓存文件里,然后再从缓存文件度回,暂时没想到同时读取修改同一个文件的方法 int ch = 0; FILE *fp_r; FILE *fp_w; printf("Enter the file name: "); gets_s(file_name, 256); if ((fp_r = fopen(file_name, "r+")) == NULL) { printf("Can't open %s as read mode!\n", file_name); } if ((fp_w = fopen(tmp_file, "w")) == NULL) { printf("Can't open %s as write mode!\n", tmp_file); } while ((ch = fgetc(fp_r)) != EOF) { ch = toupper(ch); fputc(ch, fp_w); } fclose(fp_r); fclose(fp_w); if ((fp_r = fopen(tmp_file, "r")) == NULL) { printf("Can't open %s as read!\n", tmp_file); } if ((fp_w = fopen(file_name, "w")) == NULL) { printf("Can't open %s as write!\n", file_name); } while ((ch = fgetc(fp_r)) != EOF) { fputc(ch, fp_w); } fclose(fp_r); fclose(fp_w); remove(tmp_file); printf("Done.\n"); return; } // practice 4 int p13_4(int argc, char **argv) { FILE *fp; char buf[256] = { 0 }; for (int i = 1; i < argc; i++) { if ((fp = fopen(argv[i], "r")) == NULL) { printf("Can't open file %s as read!\n", argv[i]); } printf("%s:\n", argv[i]); while (fgets(buf, 256, fp) != NULL) { fputs(buf, stdout); } printf("\n"); } printf("Done!\n"); return 0; } // practice 5 #define BUFFSIZE 4096 #define SLEN 81 void append(FILE *source, FILE *dest) { size_t bytes; static char temp[BUFFSIZE]; while ((bytes = fread(temp, sizeof(char), BUFFSIZE, source)) > 0) { fwrite(temp, sizeof(char), bytes, dest); } } char *s_gets(char *st, int n) { char *ret_val; char *find; ret_val = fgets(st, n, stdin); if (ret_val) { find = strchr(st, '\n'); if (find) { *find = '\0'; } else { while (getchar() != '\n') { continue; } } } return ret_val; } void p13_5(int argc, char **argv) { FILE *fa, *fs; int files = 0; int ch; if (argc <3) { printf("Usage: %s dest_file source1_file ...\n", argv[0]); return; } if ((fa = fopen(argv[1], "a+")) == NULL) { fprintf(stderr, "Can't open %s\n", argv[1]); exit(EXIT_FAILURE); } if (setvbuf(fa, NULL, _IOFBF, BUFFSIZE) != 0) { fputs("Can't create output buffer\n", stderr); exit(EXIT_FAILURE); } for (int i = 2; i < argc; i++) { if (strcmp(argv[1], argv[i]) == 0) { fputs("Can't append file to itself\n", stderr); } else if ((fs = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "Can't open %s\n", argv[i]); } else { if (setvbuf(fs, NULL, _IOFBF, BUFFSIZE) != 0) { fputs("Can't create input buffer\n", stderr); continue; } append(fs, fa); if (ferror(fs) != 0) { fprintf(stderr, "Error in reading file %s.\n", argv[i]); } if (ferror(fa) != 0) { fprintf(stderr, "Error in writing file %s.n", argv[1]); } fclose(fs); files++; printf("File %s appended.\n", argv[i]); } } printf("Done appending. %d files appended.\n", files); rewind(fa); printf("%s contents:\n", argv[1]); while ((ch = getc(fa)) != EOF) { putchar(ch); } puts("Done displaying!\n"); fclose(fa); return; } // practice 6 #define LEN 40 void p13_6(void) { FILE *in, *out; int ch; char out_file[LEN]; char in_file[LEN]; int count = 0; printf("Enter the file name:"); gets_s(in_file, LEN); if ((in = fopen(in_file, "r")) == NULL) { fprintf(stderr, "I couldn't open the file\"%s\"", in_file); exit(EXIT_FAILURE); } strncpy(out_file, in_file, LEN-5); out_file[LEN - 5] = '\0'; strcat(out_file, ".red"); if ((out = fopen(out_file, "w")) == NULL) { fprintf(stderr, "Can't creat output file.\n"); exit(3); } while ((ch = getc(in)) != EOF) { if (count++ % 3 == 0) putc(ch, out); } if (fclose(in) != 0 || fclose(out) != 0) { fprintf(stderr, "Error in closing files\n"); } return; } // practice 7 void p13_7(void) { FILE *file1, *file2; char file_name1[128]; char file_name2[128]; char buf1[256] = { 0 }; char buf2[256] = { 0 }; printf("Enter the first file name:"); gets_s(file_name1, 128); printf("Enter the second file name:"); gets_s(file_name2, 128); if ((file1 = fopen(file_name1, "r")) == NULL) { printf("Can't open %s\n", file_name1); } if ((file2 = fopen(file_name2, "r")) == NULL) { printf("Can't open %s\n", file_name2); } while ((feof(file1) == 0) || (feof(file2) == 0)) { if (fgets(buf1, 256, file1) != NULL) { int length = strlen(buf1); // 要实现B情况,添加如下代码 /* if (buf1[length - 1] == '\n') { buf1[length - 1] = '\0'; } */ fputs(buf1, stdout); } if (fgets(buf2, 256, file2) != NULL) { fputs(buf2, stdout); } } return; } // practice 8 void p13_8(int argc, char **argv) { char find_ch = 0; char ch = 0; char input[256] = { 0 }; int repeat_time = 0; FILE *fp; if (argc < 2) { printf("Usage: %s <character> [file_name1 ....]", argv[0]); return; } find_ch = argv[1][0]; if (argc == 2) { printf("Enter the string you wanted to analysize: "); gets_s(input, 256); int input_length = strlen(input); for (int i = 0; i < input_length; i++) { if (find_ch == input[i]) { repeat_time++; } } printf("In string \"%s\", %c have appeared %d times!\n", input, find_ch, repeat_time); } else { for (int i = 2; i < argc; i++) { repeat_time = 0; if ((fp = fopen(argv[i], "r")) == NULL) { printf("Can't open %s\n", argv[i]); continue; } while ((ch = fgetc(fp)) != EOF) { if (ch == find_ch) { repeat_time++; } } printf("In file %s, %c have appeared %d time!\n", argv[i], find_ch, repeat_time); } } return; } // practice 9 // 暂时的想到的解决办法是将单词读出来获取前面的标号,但是这样好像不够优雅 #define MAX 41 void p13_9(void) { FILE *fp; char words[MAX]; int n_word = 0; if ((fp = fopen("wordy", "a+")) == NULL) { fprintf(stdout, "Can't open\"wordy\" file.\n"); exit(EXIT_FAILURE); } rewind(fp); while (fgets(words, MAX, fp) != NULL) { n_word++; } puts("Enter words to add to the file; press the #"); puts("key at the begining of a line to terminate."); while ((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#')) { fprintf(fp, "%d:%s\n", ++n_word, words); } puts("File contents:"); rewind(fp); while (fscanf(fp, "%s", words) == 1) { puts(words); } puts("Done"); if (fclose(fp) != 0) { fprintf(stderr, "Error closing file\n"); } return; } // practice 10 void p13_10(void) { char file_name[256]; char content[256]; int file_seek = 0; FILE *fp; printf("Enter the file name:"); fscanf(stdin, "%s", file_name); // scanf("%s", file_name); if ((fp = fopen(file_name, "r")) == NULL) { printf("Can't open %s file.\n", file_name); } while (1) { printf("Enter the location of file to print ( <0 to terminate) :"); if (fscanf(stdin, "%d", &file_seek) != 1 || file_seek < 0) { break; } fseek(fp, file_seek, SEEK_SET); fgets(content, 256, fp); // fscanf(fp, "%s", content); // 奇怪,下面这两个打印方法都没有办法打印 (好吧,我知道了,我这是输出啊,应该用的是stdout,汗....) // fprintf(stdin, "%s", content); // fputs(content, stdin); printf("%s", content); } puts("Done"); fclose(fp); return; } // practice 11 void p13_11(int argc, char **argv) { char content[256]; FILE *fp; if (argc != 3) { printf("Usage: %s <string> <file_name>\n", argv[0]); exit(EXIT_FAILURE); } if ((fp = fopen(argv[2], "r")) == NULL) { printf("Can't open file %s.\n", argv[2]); } while ((fgets(content, 256, fp)) != NULL) { if (strstr(content, argv[1]) != NULL) { fputs(content, stdout); } } } // practice 12,13,14 // 剩下的就不写了,那个数组600个数字,全部输完都成斗鸡眼了 int main(int argc, char **argv) { // p13_10(); p13_11(argc, argv); while (getchar()); return 0; } |
此文为博主原创文章,转载请注明出处
关于12题可以用随机函数 生成数字存在文档里
#include
#include
int main()
{
FILE *p;
p=fopen("F:\\data2.txt","w");
for(int i=0;i<600;i++)
{
fprintf(p,"%-5d",rand()%10);
if((i+1)%30==0)
{
fprintf(p,"%-5d\n",rand()%10);
}
}
fclose(p);
return 0;
}
第9题我是查找每行开头的序号然后获取序号,比你这直接计算行数蠢多了。。。
毕竟那是最直接的办法啊
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 0 4 5 2 0 0 0 0 0 0 0 0
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 4 5 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 1 8 5 0 0 0 4 5 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 4 5 2 0 0 0 0 0
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 3 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 2 2 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 3 3 0 0 0 0 0 0 5 8 9 9 8 5 0 5 6 1 1 1 1 6 5 0 0 0
0 0 0 0 4 4 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 5 5 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
帮你贴过来了,不用谢
所以成斗鸡眼没?
好人一生平安
哈哈 就是来找它的! 感谢!
好人一生平安,就是来找它的
第7题的B,要是文件1比2大后面的每行不就全部在一起了吗
第二题运行时会出现Debug Assertion Failed ,返回can’t open是怎么回事 ????
是不是文件打开失败了?确定文件存在吗?
第7题,关于添加代码实现 B 的内容,
如果是分步进行的话,应该要再添加两个rewind(),
但如果是同步进行,演示效果会不会受影响呢?
第7题 关于添加代码以实现 B 的内容,
如果分步执行 A ,B,应该添加两个rewind(),
但如果同步执行,演示效果是否会受影响呢?
你可以试一下,我这边没有试过这种情况
第三题 可以用用两个文件指针打开同一个文件,一个读,一个写。 也可以用一个文件指针同时读写,但是要在每次写入之前设置文件位置指针fseek(fp, -1, SEEK_CUR), 写入之后要重新设置指针到当前位置 fseek(fp, 0, SEEK_CUR)。
你有试过这种方法吗?我当时有试过这样做,但是貌似以写的方式打开的话会将文件清空
兄dei,这种方法你试过吗?用写打开文件直接清空了呀,麻烦指教一下
char *tmp_file = "tmp.txt";这行代码晒什么意思呢?求解答
第九题用 fseek() 移动光标获取文件最后一个单词的编号,然后用 atoi() 将编号转为数值。(编程环境: gcc 版本 9.3.0 (Arch Linux 9.3.0-1))
#include
#include
#include
#include
#define MAX 41
#define LIMIT 1024 //单词编号上限
int main(void)
{
FILE *fp; //指向目标文件
char words[MAX]; //单词最大字符数
int num = 0; //单词编号
char num_s[LIMIT]; //单词编号字符串,方便调用atoi()函数
long len = 0; //文件长度
char ch; //存储从文件中读取字符
int i; //计数器
char temp; //临时存储字符
if ((fp = fopen("wordy", "ab+")) == NULL)
{
fprintf(stderr, "Can't open \"wordy\" file.\n");
exit(EXIT_FAILURE);
}
fseek(fp, 0L, SEEK_END); //光标定位到文件末尾
len = ftell(fp);
for (long count = 1; count < len; count++)
{ //获取打开的文件中最末尾单词的标号
fseek(fp, -count, SEEK_END); //光标回退
ch = getc(fp);
if (ch == ']')
i = 0;
if (isdigit(ch))
num_s[i++] = ch;
if (ch == '[')
break;
}
for (int j = 0; j < i / 2; j++)
{ //将倒置的单词编号调整为正确值
temp = num_s[j];
num_s[j] = num_s[i - j - 1];
num_s[i - j - 1] = temp;
}
num = atoi(num_s);
puts("Enter words to add to the file; press the #");
puts("key at the beginning of a line to terminate.");
while ((fscanf(stdin, "%s", words) == 1) && (words[0] != '#'))
fprintf(fp, "[%d]%s\n", ++num, words);
puts("File contents:");
rewind(fp);
while (fscanf(fp, "%s", words) == 1)
puts(words);
if (fclose(fp) != 0)
fprintf(stderr, "Error closing file.\n");
return 0;
}
大神好 ,我想问一下我在每次打开文件时,获取每个单词的序号的代码我是这样写的,结果导致了只 用第一次运行程序时可以添加单词,后面再运行就添加不了单词,我很苦恼 ,不知道为什么,请大神帮忙看看
我写入单词时在单词前面加了“[单词对应序号]”
#include
#include
int main()
{
FILE *fp;
char words[50];
int count, ch; //编号
if((fp = fopen("a.txt", "ab+")) == NULL)
{
fprintf(stderr, "Open of %s is fail!", "a.txt");
exit(1);
}
rewind(fp);
//找到上次最后一个编号
while( (ch = getc(fp)) != '[' && ch != EOF)
continue;
if(ch == EOF)
count = 0;
else
{
ch = getc(fp);
count = atoi(ch);
}
//添加单词到文件中
puts("Enter words to add to the file; press #");
puts("to quit.");
while( (fscanf(stdin, "%s", words) == 1) && (words[0] != '#') )
{
printf("%d***", count);
fprintf(fp, "[%d]%s ", ++count, words); //将读取的单词输出到文件
}
puts("File contents:");
rewind(fp); //回到文件开始
//显示文件内容
while(fscanf(fp, "%s", words) == 1)
puts(words);
puts("done!");
if(fclose(fp) != 0)
{
fprintf(stderr, "Error in closing file!");
exit(1);
}
return 0;
}