zoukankan      html  css  js  c++  java
  • 文件的输入输出

    模板

    #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!
    ");
            exit(1);
        }
    
        if((wfp=fopen("D:\out.txt","w"))==NULL)
        {
            printf("cannot open2!
    ");
            fclose(rfp);
            exit(2);
        }
    
        dopack(rfp,wfp);
    
        fclose(wfp);
        fclose(rfp);
        return 0;
    }
    
    
    







  • 相关阅读:
    算法训练 表达式计算
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    基础练习 十六进制转十进制
    New ways to verify that Multipath TCP works through your network
    TCP的拥塞控制 (Tahoe Reno NewReno SACK)
    Multipath TCP Port for Android 4.1.2
    How to enable ping response in windows 7?
    NS3
    Multipath TCP Port for Android
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431999.html
Copyright © 2011-2022 走看看