当前位置:网站首页>C语言实验十五 文件
C语言实验十五 文件
2022-08-03 23:37:00 【Meteor.792】
一、实验目的
(一)掌握文件以及缓冲文件系统、文件指针的概念;
(二)学会使用文件打开、关闭、读、写等文件操作函数;
(三)学会用缓冲文件系统对文件进行简单的操作。
二、预习要求
文件类型指针
文件的打开与关闭 文件的读写操作函数
三、实验内容
将一个磁盘文件中的信息复制到另一个磁盘文件中。
#include <stdio.h>
#include <stdlib.h>
void main( )
{
FILE*in,*out ; // 定义文件指针
char ch,infile[1 0],outfile[1 0];
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("Enter the outfile name:\n");
scanf("%s",outfile);
if((in=fopen(infile," r"))==NULL)
{ // 判断文件是否正确读操作
printf("cannot open infile\n"); exit(0);
}
if((out=fopen(outfile,"w"))==NULL)
{ // 判断文件是否正确写操作
printf("cannot open outfile\n"); exit(0);
}
//判文件是否结束,如果不结束,则读文件in 的内容写入到文件out 之中
while(!feof(in)) fputc(fgetc(in),out);
fclose(in); //关闭文件
fclose(out);
}运行情况如下:
Enter the infile name:
file1.c(输入原有磁盘文件名)
Enter the outfile name:
file2.c(输入新复制的磁盘文件名)程序运行结果是将 fule1. c 文件中的内容复制到 fule2. c 中去。可以用下面命令验证:
c:\>type file1.c
computer and c (file1.c中的信息)
c:\>type file2.c
computer and c (file2.c 中的信息)以上程序是按文本文件方式处理的。也可以用此程序来复制一个二进制文件,只需 将两个 fopen 函数中的“r” 和 “w”分别改为“rb” 和 “wb” 即可。
分析以上各程序的算法(用框图表示),解释产生该结果现象的相关知识点及实现语句。
阅读以下程序,回答后面的问题。
#include <stdio.h>
void main(void)
{
short a=0x253f,b=0x7b7d; char ch;
FILE *fp1,*fp2;
fp1=fopen("c:\\abc1.bin","wb+");
fp2=fopen("c:\\abc2.txt","w+");
fwrite(&a,sizeof(short), 1 ,fp1);
fwrite(&b,sizeof(short), 1 ,fp 1);
fprintf(fp2,"%hx %hx",a,b); rewind(fp 1); rewind(fp2);
while((ch = fgetc(fp1)) != EOF) putchar(ch);
putchar('\n');
while((ch = fgetc(fp2)) != EOF) putchar(ch);
putchar('\n');
fclose(fp 1);
fclose(fp2);
}(1)请思考程序的输出结果, 然后通过上机运行来加以验证。
(2)将两处 sizeof(short)均改为 sizeof(char)结果有什么不同,为什么?
(3)将 fprintf(fp2,"%hx %hx",a,b) 改为 fprintf(fp2,"%d %d",a,b)结果有什么不同。
四、实验注意事项
注意文件打开的不同方式。
边栏推荐
- 雅思大作文写作模版
- BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
- 七夕?new一个对象
- Redis persistence method
- XSLT – 编辑 XML概述
- V8中的快慢数组(附源码、图文更易理解)
- 2021年数据泄露成本报告解读
- ros mavros stereo读取rosbag并记录IMU和图片到文件夹
- Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
- Shell 用法梳理总结
猜你喜欢

初始 List 接口

Jmeter-参数化

Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan

Unity 截取3D图像 与 画中画PIP的实现

- the skip/skipif Pytest learning
![Scala basics [regular expressions, framework development principles]](/img/be/6531f32494b8516cd7031223d14bb2.png)
Scala basics [regular expressions, framework development principles]

冰河又一MySQL力作出版(文末送书)!!

CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply

响应式织梦模板塑身瑜伽类网站

Scala基础【正则表达式、框架式开发原则】
随机推荐
With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
雅思大作文写作模版
智能管理PoE交换机
Creo9.0 绘制中心线
使用unbound在RHEL7上搭建DNS服务
Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
汉字风格迁移---结合本地和全局特征学习的中文字体迁移
我的祖国
Unity 截取3D图像 与 画中画PIP的实现
响应式织梦模板餐饮酒店类网站
ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例
用队列模拟实现栈
P1449 后缀表达式
【OpenCV图像处理】 图像拼接技术
RSS feeds WeChat public - feed43 asain
详谈RDMA技术原理和三种实现方式
Flutter教程之为什么 Flutter 是创业的最佳选择?
初始 List 接口
rosbridge-WSL2 && carla-win11
一文搞定 SQL Server 执行计划