一、选择题
1.AB
2~4 C D A D
二、填空题
1.数据长久保存
2.文本 二进制
3.feof()
4.第一空:"r" ,第二空:if(fgetc(fp)!=EOF)
5.用户输入两个文件进行文件复制。
三、程序设计题
P323页
第一题
#include<stdio.h>//习题12.1 统计文本文件中各类字符个数
#include<stdlib.h>
int main(void)
{
FILE *fp;
int letter=0,digit=0,other=0;
char ch;
if((fp=fopen("wenben.txt","r"))==NULL){
printf("文件打开失败!");
exit(0);
}
ch=fgetc(fp);
while(!feof(fp)){
if(ch!=EOF){
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
letter++;
}else if(ch>='0'&&ch<='9'){
digit++;
}else other++;
}
ch=fgetc(fp);
}
fclose(fp);
printf("文件中有字母%d个,数字%d个,其他字符%d个",letter,digit,other);
return 0;
}
文件:
运行结果:
第二题
#include<stdio.h>//习题12.2 将实数写入文件
#include<stdlib.h>
#define N 80
int main(void)
{
FILE *fp;
int x[N];
int i=0;
char ch;
if((fp=fopen("shishu.txt","w"))==NULL)
{
printf("文件打开失败!");
exit(0);
}
/* printf("请输入一串实数(以-1结束):\n"); //原题,以-1结束,但运行后会乱码
do{
scanf("%lf",&x);
fprintf(fp,"%lf",x[i]);
i++;
}while(x[i]!=-1);
*/
printf("请输入一串实数(以回车结束):\n");//虽然是实数,但也是用getchar()接收的
ch=getchar();
while(ch!='\n'){
fputc(ch,fp);//写入文件,单个的
ch=getchar();
}
printf("写入完成!");
fclose(fp);
return 0;
}
运行后创建的文件:
程序运行:
第三题
#include<stdio.h>//习题12.3 两个文件中第一次出现不同的行号及列值
#include<stdlib.h>
#include<string.h>
int main(void)
{
FILE *fp1,*fp2;
char ch1,ch2;
int row=1,line=0;//行,列
if((fp1=fopen("f12-31.txt","r"))==NULL){
printf("文件f12-31打开失败。");
exit(0);
}
if((fp2=fopen("f12-32.txt","r"))==NULL){
printf("文件f12-32打开失败。");
exit(0);
}
while((!feof(fp1))&&(!feof(fp2))){
ch1=fgetc(fp1);
ch2=fgetc(fp2);
if((ch1!=EOF)&&(ch2!=EOF)){
// line++;//列加1,无论等不等于都会加一
if(ch1=='\n'){
row++;//行加1
line=0;
}
if(ch1!=ch2){
printf("第一次出现不同的行号:%d,列号:%d\n",row,line);
}else line++;
// strcmp(ch1,ch2);
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
文件:
程序运行结果:
第四题
#include<stdio.h>//习题12.4 将文件中的数据求和并写入文本文件尾
#include<stdlib.h>
int main(void)
{
FILE *fp;
int x,sum=0;
if((fp=fopen("Int_Date.txt","a+"))==NULL)//二进制文件:.dat;"ab+"
{
printf("文件打开失败。");
exit(0);
}
while(!feof(fp)){
fscanf(fp,"%d",&x);//取出 //fread(fp,sizeof(int),1,fp); 从首地址fp所存的数据中,取一个整数,取一次
sum+=x;
}
fprintf(fp,"\n文件中所以数据的累加和为:%d",sum);//fwrite(fp,sizeof(int),1,fp);
fclose(fp);
return 0;
}
第五题
#include<stdio.h>//习题12.5 输出含for的行
#include<stdlib.h>
#include<string.h>
#define N 80
main()
{
FILE *fp;
int i;
char s[N];
if((fp=fopen("test.txt","r"))==NULL){
printf("文件打开失败!");
exit(0);
}
while(!feof(fp)){
fgets(s,N,fp);//取N个字符放入s中
for(i=0;s[i]!='\0';i++)//遍历整个字符串
{
if(s[i]=='f'&&s[i+1]=='o'&&s[i+2]=='r'){
printf("文件中含for的行为:%s\n",s);//输出,如果有3行含for则会输出3次
}
}
}
fclose(fp);
}
/*fgets(s,n,fp)读取的三种情况下会自动添加'\0'
(1)读取字符达到指定个数n
(2)接收到换行符'\n'
(3)接收到文件结束标志EOF
*/
第六题
#include<stdio.h>//习题12.6 删除文件中的注释 存的是没有注释的.c文件
#include<stdlib.h>//有Bug,只适用于单行注释符'//',对'/* */'不适用
#include<string.h>
#define N 80
main()
{
FILE *fp1,*fp2;
int i;
char ch,s[N],ch1;
if((fp1=fopen("hello.txt","r"))==NULL){//只读
printf("文件打开失败!");
exit(0);
}
if((fp2=fopen("new_hello.txt","w"))==NULL){//写
printf("文件打开失败!");
exit(0);
}
while(!feof(fp1)){
ch=fgetc(fp1);//取第一个
if(ch==EOF)//文件结束符
continue;//跳过之后的继续循环
if(ch=='/'){//第一个'/'
ch=fgetc(fp1);//取之后的一个
if(ch=='/'){//第二个'/'
while(ch!='\n'&&ch!=EOF){//一行没结束,文件没结束
ch=fgetc(fp1);//继续读
}
fputc('\n',fp2);//将回车符写入新文件
}
}
else if(ch=='*'){//第一个是'/',第二个是'*'
fgets(s,N,fp1);//读三个字符到S中
while(strstr(s,"*/")==NULL){//strstr(s,t)的功能:在s中查找t首次出现的地址。循环条件,未找到(返回NULL)
fseek(fp1,1L,1);//指针从当前位置开始向前移动一位
ch=fgetc(fp1);//再次读取
// fputc(ch,fp2);//将读到的值写入新文件
}
fputc(ch,fp2);
}
else {//一个'/'都没读到,之间写入新文件
fputc(ch,fp2);
}
}
fclose(fp1);
fclose(fp2);
}
第七题(为选做题,只列了框架,可简单参考)
#include"stdio.h"//练习12-7(选做)
#include"stdlib.h"
//#include"process.h"
long size;
struct AccData{
long acctno;//账号
char acctname[15];//账号名
double charge;//金额
double balance;//余额
};
void ListAllLog(FILE *cfptr)//列出所有收支流水记录
{
struct LogData log;
fseek(cfptr,0L,SEEK_SET);
fread(&log,size,1,cfptr);
printf("logid\tlogdata \tlognote \tcharge\t\tbalance\n");
while(!feof(cfptr)){
printf("%ld\t%s\t%s\t%.2lf\t\t%.2lf\n",log.logid,log.logdate,log.lognote,log.charge,log.balance);
fread(&log,size,1,cfptr);
}
}
void QueryLastLog(FILE *cfptr)//查询最后一条记录
{
struct LogData log;
long logcount=0;
logcount=getLogcount(cfptr);
printf("logcount=%ld\n",logcount);
if(logcount>0)//有记录存在
{
fseek(cfptr,size*(logcount-1),SEEK_SET);
fread(&log,size,1,cfptr);
printf("The last log is:\n");
printf("logid:%-6ld\nlogdate:%-11s\nlognote:%-15s\n",log.logid,log.logdate,log.lognote);
printf("charge:%-10.2lf\nbalance:%-10.2lf\n",log.charge,log.balance);
}
else printf("no logs is file!\n");
}
int main(void)
{
FILE *fp;
int choice;
struct AccData Acc;
if((fp=fopen("f12-7.dat","ab+"))==NULL){
printf("can not open file cashbox.dat!\n");
exit(0);
}
size=sizeof(struct AccData);
printf("请输入您选择的查询方式:1.账号 2.账号名\n");
scanf("%d",&choice);
if(choice==1){
printf("请输入账号:");
scanf("%d",&Acc.acctno);//账号
QueryAcc()//将文件,账号传过去
}
else {
printf("请输入账号名:");
scanf("%s",Acc.acctname);//账号名
}
if(fclose(fp)){
printf("Can not close the file!\n");
exit(0);
}
return 0;
}