Sweety

Practice makes perfect
随笔 - 620, 文章 - 0, 评论 - 2, 阅读 - 20万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

文件的输入输出

Posted on   蓝空  阅读(153)  评论(0编辑  收藏  举报

模板

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cstdio>
using namespace std;
int  main()
{
  ofstream out ("D:\\temp\\output.txt",ios::out);
  srand((unsigned)time(NULL));//用于产生随机数 
  for(int j=0;j<10;j++){
  if(!out)
    cout<<"打开失败"<<endl; 
  out<<' '<<rand()%10;
    }
 out.close();//两种文件输入输出流一起用的话一定要写这句 
 
  ifstream in ("D:\\temp\\output.txt",ios::in );
    int a;
    for(int i=0;i<10;i++){
		if(!in)
	      cout<<"打开失败"<<endl; 
	in>>a;
	cout<<a<<' ';
    } 
 in.close(); 
 
}

详情见网址:http://blog.csdn.net/u014665013/article/details/45623733

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define INF f0x3f3f3f3f
#define  BUF_N 1000
using namespace std;
void pop(int s,int *buf,int c,FILE  *fp)
{
    int i;
    if(s)
        fprintf(fp,"%d %d ",c,*buf);
    else
    {
        fprintf(fp,"%d ",-c);
        for(i=0; i<c; i++)
            fprintf(fp,"%d ",buf[i]);
    }
}

void dopack(FILE *r,FILE *w)
{
    int buf[BUF_N];

    int pos=0;//下一个数字在buf中将要存放的位置
    int c=0;//当前段已读入的整数个数
    int pst;///确定符号
    int cst;

    while(fscanf(r,"%d",buf+pos)==1)
    {
        if(c==0)
        {
            c=pos=1;
            continue;
        }

        if(c==1)
        {
            pst= ( buf[0]==buf[1] ) ; ///pst记录前两个是否相同
            pos=pos+1-pst;  ///前两个不相同时  pos= 2
            c=2;
            continue;
        }
        cst = ( buf[pos-1]==buf[pos] );///第二个和第三个相同
        if( pst&&!cst ) ///前边两个相同,后面的出现不相同
        {
            pop(pst,buf,c,w);
            buf[0]=buf[1];
            c=pos=1;
            pst=cst;
        }
        else if( !pst&&cst || pos==BUF_N-1 ) ///前两个不相同   第二个第三个相同   ||  到最后
        {
            pop(pst,buf,c-1,w);
            buf[0]=buf[pos-1];
            c=2;

            if(!cst)  ///文件最后
            {
                buf[1]=buf[pos];
                pos=2;
            }
            else ///前两个不相同   第二个第三个相同
            {
                pos=1;
                pst=1 ;//填空1
            }
        }
        else ///前后都相同,前后都不相同
        {
            c++;
            if(!pst)pos++;
        }
    }//while
    if(c>0)
        pop(pst,buf,c,w);  //填空2
}
int main()
{
    FILE  *rfp;
    FILE  *wfp;

    if((rfp=fopen("D:\\in.txt","r"))==NULL)
    {
        printf("cannot open1!\n");
        exit(1);
    }

    if((wfp=fopen("D:\\out.txt","w"))==NULL)
    {
        printf("cannot open2!\n");
        fclose(rfp);
        exit(2);
    }

    dopack(rfp,wfp);

    fclose(wfp);
    fclose(rfp);
    return 0;
}









编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示