目录
(一)流程图填空
参考复习:http://t.csdn.cn/4gezI
(二.填空)第12章 文 件
12.1 基本知识点 书本p331
Ø 文件的概念
Ø 文件的分类、文件缓冲区、流和文件指针
Ø 文件打开和关闭函数
打开输入/输出文件:
if((fp=fopen("file","r"))==NULL)
{printf("cannot open this file\n");
exit(0);//关闭所有文件
}
使用exit函数要用库函数stdlib.h
关闭:fclose(fp);
Ø 文件的顺序读写函数(fgetc()/fputc()读写一个字符、fgets()/fputs()读写一个字符串、fscanf()/fprintf()格式化读写)p338、342
fgetc(fp)/fputc(ch,fp) fgets(str,n,fp)/fputs(str,fp) n-1字符+\'0' fscanf(fp,"%d,%f",&i,&f)/fprintf(fp,"%d,%6.2f",i,f)
Ø 文件的随机读写函数(fread(buffer,size,count,fp)/fwrite(buffer,size,count,fp))二进制 int型p346
Ø 文件定位操作函数(ftell()得知文件当前位置、fseek(fp,位移量,起始点)和rewind()文件位置标记指向文件开头)
Ø 文件检测函数(feof(fp))
12.2 练习题12及参考答案
1. 单项选择题
(1)若要以模式"a+"打开一个已存在的文件,则以下叙述正确的是 。
A. 文件打开时,原有文件内容不被删除,位置指针移动到文件末尾,可做追加和读操作
B. 文件打开时,原有文件内容不被删除,位置指针移动到文件首,可做重写和读操作
C. 文件打开时,原有文件内容被删除,只可做写操作
D. 以上各种说法都不正确
【答】A
(2)当已存在一个abc.txt文件时,执行函数fopen("abc.txt","r+");的功能是 。
A. 打开abc.txt文件,清除原有的内容
B. 打开abc.txt文件,只能写入新的内容
C. 打开abc.txt文件,只能读取原有内容
D. 打开abc.txt文件,可以读取和写入新的内容
【答】D
(3)fopen()函数的打开文件模式"r"和"w"的差别是 。
A. "r"可向文件写,"w"不可向文件写
B. "r"不可向文件写,"w"可向文件写
C. "r"不可由文件读,"w"可由文件读
D. 文件不存在时,"r"建立新文件,"w"出错
【答】B
(4)fopen()函数的打开文件模式为"w+"和"a+"时都可以写入数据,它们之间的差别是 。
A. "w+"时可在中间插入数据,而"a+"时只能在末尾追加数据
B. "w+"时和"a+"时只能在末尾追加数据
C. 在文件存在时,"w+"时清除原文件数据,而"a+"时保留原文件数据
D. "w+"时不能在中间插入数据,而"a+"时只能在末尾追加数据
【答】C
(5)利用fseek()函数可以实现的操作是 。
A. 能够移动文件的位置指针
B. 能够辅助实现文件的顺序读写
C. 能够辅助实现文件的随机读写
D. 以上都不对
【答】A
(6)函数调用语句fseek(fp,-10L,2);的含义是 。
A. 将文件位置指针移动到距离文件首10个字节处
B. 将文件位置指针从当前位置向后移动10字节
C. 将文件位置指针从文件末尾处向后退10字节
D. 将文件位置指针移动到离当前位置10字节处
【答】C
(7)已知函数的调用形式:fread(buffer,size,count,fp);其中buffer代表的是 。
A. 一个整型变量,代表要读入的数据项总数
B. 一个文件指针变量,指向要读的文件
C. 一个指针变量,指向要读入数据的存放地址
D. 一个存储区,存放要读的数据项
【答】C
(8)若有以下定义:
struct std
{
char num[6];
char name[8];
float mark[4];
} a[30];
FILE *fp;
设文件中以二进制形式存放有10个班的学生数据,且已正确打开,文件位置指针定位于文件首。若要从文件中读出30个学生的数据放入a数组中,以下不能实现此功能的语句是 。
A. for (i=0;i<30;i++)
fread(&a[i],sizeof(struct std),1L,fp);
B. for (i=0;i<30;i++)
fread(a+i,sizeof(struct std),1L,fp);
C. fread(a,sizeof(struct std),30L,fp);
D. for (i=0;i<30;i++)
fread(a[i],sizeof(struct std),1L,fp);
【答】选项A和B显然是正确的,选项C是将30个数据作为一个整体,通过一次调用函数fread()完成读入30个数据的要求。本题答案为D。
(9)以下程序的运行结果是 。
#include <stdio.h>
main()
{
int i,n;
FILE *fp;
if ((fp=fopen("temp","w+"))==NULL)
{
printf("不能建立temp文件\n");
return;
}
for (i=1;i<=10;i++)
fprintf(fp,"%3d",i);
for (i=0;i<5;i++)
{
fseek(fp,i*6L,SEEK_SET);
fscanf(fp,"%d",&n);
printf("%3d",n);
}
printf("\n");
fclose(fp);
}
A. 1 3 5 7 9 B. 2 4 6 8 10
C. 3 5 7 9 11 D. 1 2 3 4 5
【答】先建立temp文件,向其中写入1~10这十个数字,每个数字占3位。然后从头开始,每隔6位取出一个数字并输出。本题答案为A。
(10)以下程序的功能是____。
#include <stdio.h>
main()
{
FILE *fp;
char str[]="HELLO";
fp=fopen("PRN","w");
fputs(str,fp);
fclose(fp);
}
A. 在屏幕上显示字符串“HELLO”
B. 把字符串“HELLO”存入PRN文件中
C. 在打印机上打印出字符串“HELLO”
D. 以上都不对
【答】因PRN是设备文件,该程序是向打印机中打印出字符串。本题答案为C。
2. 填空题
(1)在C语言中,从用户角度看,文件可分为 ① 和 ② 两种;从文件的读写方式来看,文件可分为 ③ 和 ④ 两种;从文件编码的方式来看,文件可分为 ⑤ 和
⑥ 两种。
【答】①普通文件 ②设备文件 ③顺序读写文件 ④随机读写文件 ⑤ASCII码文件 ⑥二进制码文件 (①和②、③和④、⑤和⑥可不考虑次序)
(2)假设a数组的定义为:int a[10];则fwrite(&a,2,10,fp)的功能是 。
【答】将数组a中10个4字节的整数写入到由fp指向的文件中。
(3)以下程序的功能是 。
#include <stdio.h>
main()
{
FILE *fp;
fp=fopen("stud.txt","r+");
while (!feof(fp))
if (fgetc(fp)=='*')
{
fseek(fp,-1L,SEEK_CUR);
fputc('$',fp);
fseek(fp,ftell(fp),SEEK_SET);
}
fclose(fp);
}
【答】程序的功能是将stud.txt文件中所有'*'字符均替换成'$'字符。
(4)以下程序的功能是 。
#include <stdio.h>
main()
{
FILE *fp1,*fp2;
fp1=fopen("file1","a");
if (fp1==NULL)
{
printf("file1文件不能打开\n");
return;
}
fp2=fopen("file2","r");
if (fp1==NULL)
{
printf("file2文件不能打开\n");
return;
}
while (!feof(fp2))
fputc(fgetc(fp2),fp1);
fclose(fp1);
fclose(fp2);
}
【答】程序的功能是将file2文件内容添加到文件file1之后。
(5)有一个包含如下内容的abc.txt文件:
#include <stdio.h> /*include head file*/
main() /*main function*/
{
printf("Hello\n"); /*print "Hello"*/
}
在执行以下程序后生成一个名为xyz.txt的文件,该文件的内容是 。
#include <stdio.h>
main()
{
FILE *fp1,*fp2;
int c,i=0;
fp1=fopen("abc.txt","r");
fp2=fopen("xyz.txt","w");
while ((c=fgetc(fp1))!=EOF)//EOF:文件结束
if (c=='\n')
fprintf(fp2,"\n");
else
switch(i)
{
case 0: if (c=='/') i=1;
else fprintf(fp2,"%c",c);
break;
case 1: if (c=='*') i=2;
else
{
fprintf(fp2,"/%c",c);
i=0;
}
break;
case 2: if (c=='*') i=3;
break;
case 3: i=(c=='/')?0:2;
break;
}
fclose(fp1);
fclose(fp2);
}
【答】程序的功能是删除源文件(abc.txt)中注释信息并存放到目标文件(xyz.txt)中。所以执行本程序后xyz.txt文件的内容如下:
#include <stdio.h>
main()
{
printf("Hello\n");
}
(6)以下程序的运行结果是____。
#include <stdio.h>
#include <stdlib.h>
main()
{
int i,n;
FILE *fp;
if ((fp=fopen("temp","w+"))==NULL)
{
printf("不能建立temp文件\n");
exit(0);
}
for (i=1;i<=10;i++)
fprintf(fp,"%3d",i);
for (i=0;i<10;i++)
{
fseek(fp,i*3L,SEEK_SET);
fscanf(fp,"%d",&n);
fseek(fp,i*3L,SEEK_SET);
fprintf(fp,"%3d",n+10);
}
for (i=1;i<=5;i++)
{
fseek(fp,i*6L,SEEK_SET);
fscanf(fp,"%d",&n);
printf("%3d",n);
}
fclose(fp);
}
【答】11 13 15 17 19
(7)以下程序用于将从键盘输入的若干行字符保存在file.txt文件中。请填空。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
FILE *fp;
char str[80];
if ((fp=fopen("file.txt","w"))==NULL)
{
printf("file.txt不能打开");
exit(0);
}
while (strlen(gets(str))>0)
{
① ;
② ;
}
fclose(fp);
}
【答】①fputs(str,fp) ②fputs('\n',fp)
(三、编程)
(1)顺序结构和选择结构相关的编程
顺序结构的程序设计可能比较简单,如“给定三角形三边,运用海伦公式求解三角形面积”等;
#include<stdio.h>
#include<math.h>
double Triangle()
{
double a,b,c,s,S;
while(1)
{
printf("Please input three edge a,b,c:\n");
scanf("%lf%lf%lf",&a,&b,&c);//gets(a,b,c);/puts针对数组使用be like:puts(st[i]);
if(a+b<=c||b+c<=a||a+c<=b)
{
printf("Can not form a triangle!\n");
printf("Please reinput\n");
printf("******************************\n");
}
else
{
s=(a+b+c)/2;
S=sqrt(s*(s-a)*(s-b)*(s-c));//海伦公式
printf("The area of this triangle is:%lf",S);
break;
}
}
}
int main()
{
Triangle();
return 0;
}
运行结果:
(2)选择结构的程序设计大概率考switch;
表达式——分支结构
Switch用法实例
*(实验三)编写一个程序,实现从键盘输入年份和月份,并输出该年、该月的天数。
*(实验三)编写一个程序,实现从键盘输入日期(年、月、日),计算输出该日期是这年的第多少天。
#include<stdio.h>
#include<math.h>
void Month()//对题目理解有误,题设问该年,该月,并非该年该月有多少天,是分开而非总共
{
printf("Please input year and month:(eg:2023 1)\n");
int year,month,day;
scanf("%d%d",&year,&month);
if(year%400==0||year%4==0&&year%100!=0)//闰年//if(year%400==0&&year%4==0&&year%100!=0)
printf("\nThis year have 366 days!\n");
else
printf("\nThis year have 365 days!\n");
switch(month)
{
case 1://是冒号!!
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
printf("This month have 31 days!\n");
break;
}
case 4:
case 6:
case 9:
case 11:
{
printf("This month have 30 days!\n");
break;
}
case 2:
{
if(year%4==0&&year%100!=0||year%400==0)
printf("This month have 29 days!\n");
else
printf("This month have 28 days!\n");
break;
}
default :printf("ERROR MONTH INPUT\n");
}
}
void Day()
{
printf("Please input year month and day:(eg:2023 1 7)\n");
int year,month,day,s=0;
scanf("%d%d%d",&year,&month,&day);
switch(month)//switch语句的执行是找匹配项,找到匹配项就执行case中的内容,如果这个匹配项后面没有break,则接着执行后面的case项。
{
case 12: s+=30;
case 11: s+=31;
case 10: s+=30;
case 9: s+=31;
case 8: s+=31;
case 7: s+=30;
case 6: s+=31;
case 5: s+=30;
case 4: s+=31;
case 3: s+=28;
case 2: s+=31;
case 1: s+=day;
}
if((year%4==0 && year%100!=0 || year%400==0) && month>=3)
s++;
printf("The date is the %d day of the year\n",s);
}
int main()
{
Month();
Day();
return 0;
}
运行结果:
Please input year and month:(eg:2023 1)
2023 2This year have 365 days!
This month have 28 days!
Please input year month and day:(eg:2023 1 7)
2023 2 22
The date is the 53 day of the year--------------------------------
Process exited after 22.4 seconds with return value 0
请按任意键继续. . .
(3)循环结构
循环结构会考察如“金字塔”等图形的输出
二维数组与字符串——杨辉三角(金字塔)
#include<stdio.h>
#define M 20
void yh(int b[M][M],int n)
{
int i, j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j||j==n) b[i][j]=1;
else b[i][j]=b[i-1][j]+b[i-1][j-1];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("%4d",b[i][j]);
}
printf("\n");
}
}
int main()
{
int n=0;
int a[M][M]={0};
scanf("%d",&n);
yh(a,n);
return 0;
}
运行结果:
结构体——链表
(4)指针与数组的结合
p291
(5)递归函数(字符串)
不会考“累加”“阶乘”等简单递归,但也不会难到如“汉诺塔”“八皇后”等问题
拓展复习:
使用文件方式中涉及二进制文本概念:http://t.csdn.cn/1ISEc
putchar(10)=换行:http://t.csdn.cn/Qflpi
getchar():不赋值给任何变量,可消化字符