c+和c语言文件操作全高效与简洁_第1页
c+和c语言文件操作全高效与简洁_第2页
c+和c语言文件操作全高效与简洁_第3页
c+和c语言文件操作全高效与简洁_第4页
c+和c语言文件操作全高效与简洁_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include stdio.h?int main()FILE *fp,*f;?int a,b,c;?fp=fopen(apple1.txt,r);?f=fopen(apple2.txt,w);?fscanf(fp,%d%d%d,&a,&b,&c);?a=5;?b=3;?fprintf(f,%d?%d,a+b+c,b);?fclose(fp);?fclose(f);?return 0;2.txt文件,按例二、新建一个名为1.txt的文件,里面按如图1存储6个数据,然后在同一目录下建立一图2格式输出这六个数据。#include stdio.hint main()FILE *fp,*fpp;int

2、 a,b,c,d,e,f;fp=fopen(1.txt,r);fpp=fopen(2.txt,w);fscanf(fp,%d%d%d%d%d%d,&a,&b,&c,&d,&e,&f);fprintf(fpp,%d %d %d %d %d %d,a,b,c,d,e,f);fclose(fp);fclose(fpp);return 0;C+常用:#include ifstream filein( data.in );/ 定义一个文件输入流ofstream fileout( data.out ); cout fileout都要改成filein, cout都要改成fileoutc语言常用:freope

3、n( date.in , r ,stdin); /重定向所有标准的输入为文件输入freopen( date.out , w ,stdout);重定向所有标准的输出为文件输出fclose(stdout); / 输出结束freopen( date.in , r ,stdin); /重定向所有标准的输入为文件输入freopen( date.out , w ,stdout);重定向所有标准的输出为文件输出fclose(stdout); / 输出结束第一句的意思就是文件输入,以读状态,去替换标准的输入以上如果只是规定用文件输入输出的某一种,那么就只用其中的一种方法一:最简单的main()freopen(

4、in.txt,r,stdin);/ 从 in.txt 中读取数据freopen(out.txt,w,stdout);/ 输出到 out.txt 文件方法二:速度比第一种快main()FILE *in;FILE *out;in=fopen(a.txt,r); /指针指向输入文件out=fopen(b.txt,w);/ 输出文件格式fscanf(in,%d,&temp);/读取文件中的数据fprintf(out,%d ,temp);fclose(in);/关闭文件流fclose(out);/关闭文件流#include #include using namespace std; int main()

5、int i,j;int n,m;static int a1024,v1024,w1024;scanf(%d%d,&n,&m);for(i=0;in;i+)scanf(%d”,&vi);for(i=0;in;i+)scanf(%d”,&wi);for(i=0;i=vi;j-)aj=max(aj,aj-vi+wi);printf(%d,am);return 0;)标准c语言#include #include using namespace std; const int MAXN = 10000; int aMAXN = 0; int main()FILE *fin, *fout;fin = fre

6、open(y.in, r, stdin);fout = freopen(y.out, w, stdout); int n = 0;fscanf(fin, %d, &n);for(int i = 0; i n; +i)fscanf(fin, %d, &ai);sort(a, a + n);for(int i = 0; i n; +i)fprintf(fout, %d , ai);fclose(fin);fclose(fout);return 0;标准 c+#include #include using namespace std; const int MAXN = 10000; int aMAXN = 0;int main()ifstream fin(x.in);ofstream fout(x.out);int n = 0;fin n;for(int i = 0;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论