实验7
task1
#include <stdio.h> #define N 80 #define M 100 typedef struct { char name[N]; // 书名 char author[N]; // 作者 } Book; void write(); void read(); int main() { printf("测试1: 把图书信息写入文本文件\n"); write(); printf("\n测试2: 从文本文件读取图书信息, 打印输出到屏幕\n"); read(); return 0; } void write() { Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, {"《灯塔》", "克里斯多夫.夏布特"}, {"《人的局限性》", "塞缪尔.约翰生"}, {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, {"《大地之上》", "罗欣顿·米斯特里"}, {"《上学记》", "何兆武"}, {"《命运》", "蔡崇达"} }; int n, i; FILE *fp; // 计算数组x中元素个数 n = sizeof(x) / sizeof(x[0]); // 以写的方式打开文本文件data1.txt fp = fopen("data1.txt", "w"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to write\n"); return; } // 将结构体数组x中的图书信息格式化写到fp指向的文件data1.txt for(i = 0; i < n; ++i) fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); fclose(fp); } void read() { Book x[M]; int i, n; FILE *fp; // 以读的方式打开文本文件data1.txt fp = fopen("data1.txt", "r"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to read\n"); return; } // 从文件中读取图书信息,保存到结构体数组x中 i = 0; while(fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) ++i; // 将图书信息打印输出到屏幕上 n = i; for(i = 0; i < n; ++i) printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); fclose(fp); }

2
#include <stdio.h> #define N 80 #define M 100 typedef struct { char name[N]; // 书名 char author[N]; // 作者 } Book; void write(); void read(); int main() { printf("测试1: 把图书信息以数据块方式写入二进制文件\n"); write(); printf("\n测试2: 从二进制文件读取图书信息, 打印输出到屏幕\n"); read(); return 0; } void write() { Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, {"《灯塔》", "克里斯多夫.夏布特"}, {"《人的局限性》", "塞缪尔.约翰生"}, {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, {"《大地之上》", "罗欣顿·米斯特里"}, {"《上学记》", "何兆武"}, {"《命运》", "蔡崇达"} }; int n, i; FILE *fp; // 计算数组x中元素个数 n = sizeof(x) / sizeof(x[0]); // 以写的方式打开二进制文件data2.dat fp = fopen("data2.dat", "wb"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to write\n"); return; } // 将结构体数组x中的图书信息以数据块方式写入二进制文件data2.dat fwrite(x, sizeof(Book), n, fp); fclose(fp); } void read() { Book x[M]; int i, n; FILE *fp; // 以读的方式打开二进制文件data2.dat fp = fopen("data2.dat", "rb"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to read\n"); return; } // 从二进制文件data2.dat以数据块方式读取图书信息存储到结构体数组x i = 0; while(fread(&x[i], sizeof(Book), 1, fp) == 1) ++i; // 在屏幕上打印输出 n = i; for(i = 0; i < n; ++i) printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); fclose(fp); }

fopen,fclose是文件指针的打开和关闭,wb和rb控制了文件的打开方式,二进制只写和只读
fwrite(x, sizeof(Book), n, fp);这句话中,x是地址头,sizeof是每个块的内存,而n是这些块的个数,fp是文件指针
同理,fread是相对应的
3.task
#include <stdio.h> #define N 100 #define M 80 void write(); void read_str(); void read_char(); int main() { printf("测试1: 把一组字符信息以字符串方式写入文本文件\n"); write(); printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n"); read_str(); printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n"); read_char(); return 0; } void write() { char *ptr[] = { "Working\'s Blues", "Everything Will Flow", "Streets of London", "Perfect Day", "Philadelphia"}; int i, n; FILE *fp; fp = fopen("data3.txt", "w"); if(fp == NULL) { printf("fail to open file to write\n"); return; } n = sizeof(ptr)/sizeof(ptr[0]); for(i = 0; i < n; ++i) { fputs(ptr[i], fp); fputs("\n", fp); } fclose(fp); } void read_str() { char songs[N][M]; int i, n; FILE *fp; fp = fopen("data3.txt", "r"); if(fp == NULL) { printf("fail to open file to read\n"); return; } i = 0; while(i < N && (fgets(songs[i], M, fp) != NULL)) ++i; n = i; for(i = 0; i < n; ++i) printf("%d. %s", i+1, songs[i]); fclose(fp); } void read_char() { int ch; FILE *fp; fp = fopen("data3.txt", "r"); if(fp == NULL) { printf("fail to open file to read\n"); return; } while((ch = fgetc(fp)) != EOF) putchar(ch); fclose(fp); }
fputs(char *,FILE *stream),将str指向的字符串写入流文件中
fgets(char *,int ,FILE *stream),从流文件中读取最多num-1个字符,将他们存入str中
putchar(char)
fgetc(FILE *stream)
fputc(char,FILE *stream)向流文件中输入

1. 当前路径下生成文本文件data3.txt(尝试用文本编辑器打开,查看其内容)

1. 代码line25字符串 "Working\'s Blues"输出时为什么没有显示反斜杠\?
因为\‘构成了转义字符
2. read_str() 函数中,条件 i < N 用途是?
读取每一行
task4
#include<stdio.h> #include<stdlib.h> #include<string.h> #define M 40 int main() { FILE *fp = fopen("C:\\Users\\HW\\CLionProjects\\untitled\\test.txt", "w"); char str[3][M]; strcpy(str[0], "0123456789-0123456789\n"); strcpy(str[1], "nuist2025\n"); strcpy(str[2], "cosmos galaxy\n"); for(int i = 0; i < 3; i++) { fputs(str[i], fp); } fclose(fp); fp = fopen("test.txt", "r"); char ch; int count1 = 0,count2 = 0; while((ch = fgetc(fp)) != EOF) { if(ch == '\n') { count1++; } if(!(ch == '\n'|| ch == ' ') ) { count2++; } } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: \t\t\t%d\n",count1); printf("字符数(不计空白符):\t%d",count2); }

#include <stdio.h> #include <stdlib.h> #include <time.h> #include<string.h> #define N 100 int isflag(int *flag,int i) { for(int j=0;j<i-1;j++) { for(int k=j+1;k<i;k++) { if(flag[j]==flag[k]) { return 0; } } } return 1; } int main() { FILE *fp; srand(time(NULL)); int n = 5,count = 0,flag[5] = {0}; char s[N][N]; char t[5][N]; fp = fopen("C:\\Users\\HW\\Desktop\\C语言\\实验7数据文件及部分代码_gbk\\list.txt","r"); if(fp == NULL) { printf("File could not be opened\n"); exit(1); } for(int i=0;fgets(s[i],N,fp)!=NULL;i++) { count++; } fclose(fp); for(int i=0;i<5;) { int randNum = rand()%count; flag[i] = randNum; if (isflag(flag,i)==0){continue;} if(isflag(flag,i)==1) { strcpy(t[i],s[randNum]); i++; } } char str[N]; char str2[N] = "C:\\Users\\HW\\Desktop\\C语言\\实验7数据文件及部分代码_gbk\\"; scanf("%s",str); strcat(str2,str); fp = fopen(str2,"w"); if(fp == NULL) { printf("File could not be opened\n"); } for(int i=0;i<5;i++) { fprintf(fp,"%s\n",t[i]); } fclose(fp); }

浙公网安备 33010602011771号