c++第十章 习题

  • 格式:doc
  • 大小:39.00 KB
  • 文档页数:4

第十章 习题
一、选择题
1 2 3 4 5 6 7 8 9 10
A B C D D C B A

二、填空题
1、顺序、随机
2、ASCII、二进制
3、n-1
4、FILE *fp;
5、对文件操作出错
三、程序设计题
1、从键盘输入一个字符串(字符串以#结束),将此字符串输入到文件string中,再从文件中读出字符串显
示在屏幕上。(使用fgetc和fputc函数)
答案
/*
* 第10章
* 1、从键盘输入一个字符串(字符串以#结束),
* 将此字符串输入到文件string中,
* 再从文件中读出字符串显示在屏幕上。(使用fgetc和fputc函数)
*/
#include
#include
main()
{
FILE *fp;
char str[100], ch;
int i = 0;
if((fp = fopen("string", "w")) == NULL) {
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
/*从键盘接收字符串存放于str中*/
printf("input a string: \n");
scanf("%s", str);
/*将字符串str中的各个字符写入文件*/
while(str[i] != '#' && str[i] != '\0') {
fputc(str[i], fp);
i++;
}
fclose(fp);
if((fp = fopen("string", "r")) == NULL)
{
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
ch = fgetc(fp);
/*从文件中依次读出字符串的各字符显示在屏幕上*/
while(ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
}
2、从键盘输入一个字符串(字符串以#结束),将此字符串输入到文件string中,再从文件中读出字符串显
示在屏幕上。(使用fgets和fputs函数)
答案
/*
* 第10章
* 2、从键盘输入一个字符串(字符串以#结束),
* 将此字符串输入到文件string中,
* 再从文件中读出字符串显示在屏幕上。(使用fgets和fputs函数)
*/
#include
#include
#include
main()
{
FILE *fp;
char str[100];
int i = 0;
if((fp = fopen("string", "w")) == NULL) {
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
scanf("%s", str); /*从键盘接收字符串存放于str中*/
str[strlen(str)-1] = '\0'; /*忽略字符#*/
fputs(str, fp); /*将字符串写入文件*/
fclose(fp);
if((fp = fopen("string", "r")) == NULL) {
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
fgets(str, strlen(str) + 1, fp); /*从文件中读出字符串存放于str中*/
printf("%s\n", str); /*在屏幕上显示字符串*/
fclose(fp);
}
3、编程序将10名学生(学号分别为1001、1002、1003……1010)的信息通过键盘输入,顺序写入文
件。并实现通过键盘输入学号,能够随机读取文件信息,进行相应学生的信息的查询功能。
答案
/*
* 第10章
* 3、编程序将10名学生(学号分别为1001、1002、1003……1010)的信息通过键盘输入,
* 顺序写入文件。并实现通过键盘输入学号,能够随机读取文件信息,进行相应学生的信息的查询功
能。
*/
#include
#include
struct stu {
int num;
char name[30];
float score;
} student[10], student_search;
int main()
{
int i, num_search;
FILE *fp;
/*从键盘输入10名学生数据存放于student中*/
for(i = 0; i < 10; i++) {
student[i].num = 1000 + i + 1;
printf("\ninput student %d: \n", i + 1);
printf("num: %d\n", student[i].num);
printf("name: ");
scanf("%s", student[i].name);
printf("score: ");
scanf("%f", &student[i].score);
}
if((fp = fopen("student_search", "wb")) == NULL) {
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
/*将10名学生数据依次写入文件*/
for(i = 0; i < 10; i++) {
fwrite(&student[i], sizeof(struct stu), 1, fp);}
fclose(fp);
/*从键盘输入要查询的学生学号*/
printf("\ninput num_search: \n");
scanf("%d", &num_search);
/*根据学号计算相应学生信息在文件中的位置*/
num_search = num_search % 100;
if((fp = fopen("student_search", "rb")) == NULL) {
printf("Cannot open file,press any key exit!");
getch();
exit(1);
}
/*将文件位置指针定位至要读的学生信息位置*/
fseek(fp, (num_search - 1)*sizeof(struct stu), 0);
/*将相应学生信息从文件中读出,存放于student_search中*/
fread(&student_search, sizeof(struct stu), 1, fp);
/*将相应学生信息显示在屏幕上*/
printf("result: \n");
printf("num: %d, name: %s, score: %f", student_search.num, student_search.name,
student_search.score);
fclose(fp);
return 0;
}