while(scanf("%s", record) > 0){
时间: 2024-06-16 21:06:10 浏览: 15
这段代码是一个循环,它会不断读取输入的字符串,并将其存储在名为record的变量中。循环会一直执行,直到scanf函数返回一个小于等于0的值,表示没有更多的输入可读取。
在每次循环迭代中,scanf函数会尝试读取一个字符串,并将其存储在record变量中。如果成功读取到字符串,则scanf函数返回1,否则返回0或负数。
这段代码通常用于从标准输入中读取多个字符串,直到遇到文件结束符或输入错误为止。
相关问题
void insertRecord() { int i=0; char num[32]; printf("请您输入您要插入的学生的学号\n(^_^)\n"); scanf("%s",num); while(i<count&&strcmp(a[i].num ,num)!=0) { i++; } if(i==count) { printf("班级:\n"); scanf("%s",a[count].class ); printf("姓名:\n"); scanf("%s",a[count].name ); printf("学号:\n"); scanf("%s",a[count].num ); printf("手机号:\n"); scanf("%s",a[count].phone1 ); printf("手机号短号:\n"); scanf("%s",a[count].phone2 ); printf("家庭住址:\n"); scanf("%s",a[count].addr ); printf("邮编\n"); scanf("%s",a[count].code ); count++; printf("插入成功!\n"); strcpy(a[count].num,num); system("pause"); return; } else { printf("该学生已存在哦,请您修改信息:(^_^)\n"); printf("班级:\n"); scanf("%s",a[i].class ); printf("姓名:\n"); scanf("%s",a[i].name ); printf("学号:\n"); scanf("%s",a[i].num ); printf("手机号:\n"); scanf("%s",a[i].phone1 ); printf("手机号短号:\n"); scanf("%s",a[i].phone2 ); printf("家庭住址:\n"); scanf("%s",a[i].addr ); printf("邮编\n"); scanf("%s",a[i].code ); printf("修改成功!\n"); system("pause"); } }是什么意思
这是一段C语言的函数代码,实现的是向一个学生信息数组中插入一条学生记录的功能。具体来说,函数会先要求用户输入要插入的学生的学号,然后在数组中查找是否已经存在该学号对应的学生记录。如果不存在,则要求用户输入该学生的各项信息,并将该学生记录插入到数组末尾;如果已经存在该学号对应的学生记录,则要求用户修改该学生的各项信息。最后,函数会输出插入/修改成功的提示信息,并暂停程序等待用户操作。函数中使用了循环、条件判断、字符串比较、数组下标访问、字符串拷贝等基本语法。
修改这段代码 void in() { int i, m = 0; char ch[2]; FILE* fp; if ((fp = fopen("d:/学生系统/student.txt", "ab+")) == NULL) { printf("can not open\n"); return; } while (!feof(fp)) { if (fread(&stu[m], LEN, 1, fp) == 1) m++; } fclose(fp); if (m == 0) printf("No record!\n"); else { system("cls"); show(); } if ((fp = fopen("d:/学生系统/student.txt", "wb")) == NULL) { printf("can not open\n"); return; } for (i = 0; i < m; i++) fwrite(&stu[i], LEN, 1, fp); printf("please input(y/n):"); scanf("%s", ch); while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) { printf("学号:"); scanf("%d", &stu[m].num); for (i = 0; i < m; i++) if (stu[i].num == stu[m].num) { printf("学号已存在!"); _getch(); fclose(fp); return; } printf("姓名:"); scanf("%s", stu[m].name); printf("性别:"); scanf("%s", stu[m].sex); printf("出生年:"); scanf("%d", &stu[m].year); printf("出生月:"); scanf("%d", &stu[m].month); printf("出生日:"); scanf("%d", &stu[m].day); printf("c语言:"); scanf("%f", &stu[m].C); printf("高数:"); scanf("%f", &stu[m].math); printf("大学物理:"); scanf("%f", &stu[m].physics); stu[m].sum = stu[m].C + stu[m].math + stu[m].physics; stu[m].avg = stu[m].sum / 3; if (fwrite(&stu[m], LEN, 1, fp) != 1) { printf("can not save!"); _getch(); } else { printf("%s saved!\n", stu[m].name); m++; } printf("continue?(y/n):"); scanf("%s", ch); } fclose(fp); printf("OK!\n"); }
void in() {
int i, m = 0;
char ch[2];
FILE* fp;
if ((fp = fopen("d:/学生系统/student.txt", "ab+")) == NULL) {
printf("can not open\n");
return;
}
while (fread(&stu[m], LEN, 1, fp) == 1) {
m++;
}
fclose(fp);
if (m == 0) {
printf("No record!\n");
}
else {
system("cls");
show();
}
if ((fp = fopen("d:/学生系统/student.txt", "ab")) == NULL) {
printf("can not open\n");
return;
}
printf("please input(y/n):");
scanf("%s", ch);
while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {
printf("学号:");
scanf("%d", &stu[m].num);
for (i = 0; i < m; i++) {
if (stu[i].num == stu[m].num) {
printf("学号已存在!");
_getch();
fclose(fp);
return;
}
}
printf("姓名:");
scanf("%s", stu[m].name);
printf("性别:");
scanf("%s", stu[m].sex);
printf("出生年:");
scanf("%d", &stu[m].year);
printf("出生月:");
scanf("%d", &stu[m].month);
printf("出生日:");
scanf("%d", &stu[m].day);
printf("c语言:");
scanf("%f", &stu[m].C);
printf("高数:");
scanf("%f", &stu[m].math);
printf("大学物理:");
scanf("%f", &stu[m].physics);
stu[m].sum = stu[m].C + stu[m].math + stu[m].physics;
stu[m].avg = stu[m].sum / 3;
if (fwrite(&stu[m], LEN, 1, fp) != 1) {
printf("can not save!");
_getch();
}
else {
printf("%s saved!\n", stu[m].name);
m++;
}
printf("continue?(y/n):");
scanf("%s", ch);
}
fclose(fp);
printf("OK!\n");
}
修改说明:
1. 删除了 while (!feof(fp)) 循环,因为这种方式会导致最后一个数据重复读入。
2. 修改了打开文件的方式,将 "wb" 修改为 "ab",这样可以在文件末尾添加数据。
3. 将读取文件和写入文件的操作分开,避免因为文件指针位置不正确而导致的错误。
4. 修改了文件指针的关闭方式,在函数的各个分支中都关闭了文件指针,避免了文件指针未关闭的风险。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)