zoukankan      html  css  js  c++  java
  • c++对txt文件的读取与写入 【转载】

    资料收藏,文章来自:http://blog.csdn.net/lh3325251325/article/details/4761575
    (博主看到,请谅解!)
    1. #include <iostream>  
    2. #include <iomanip>  
    3. #include <fstream>  
    4.   
    5. using namespace std;  
    6.   
    7. int main(){  
    8. char buffer[256];  
    9. ifstream myfile ("c://a.txt");  
    10. ofstream outfile("c://b.txt");  
    11.   
    12. if(!myfile){  
    13.   cout << "Unable to open myfile";  
    14.         exit(1); // terminate with error  
    15.   
    16. }  
    17. if(!outfile){  
    18.     cout << "Unable to open otfile";  
    19.         exit(1); // terminate with error  
    20.   
    21. }  
    22. int a,b;  
    23. int i=0,j=0;  
    24. int data[6][2];  
    25.   while (! myfile.eof() )  
    26.   {  
    27.      myfile.getline (buffer,10);  
    28.     sscanf(buffer,"%d %d",&a,&b);  
    29.     cout<<a<<" "<<b<<endl;  
    30.      data[i][0]=a;  
    31.      data[i][1]=b;  
    32.      i++;  
    33.   }  
    34. myfile.close();  
    35.   for(int k=0;k<i;k++){  
    36.       outfile<<data[k][0] <<" "<<data[k][1]<<endl;  
    37.      cout<<data[k][0] <<" "<<data[k][1]<<endl;  
    38.   }  
    39.   
    40. outfile.close();  
    41. return 0;  
    42. }  
    43.   
    资料收藏,文章来自:http://blog.csdn.net/lh3325251325/article/details/4761575
    (博主看到,请谅解!)

    无论读写都要包含<fstream>头文件

    读:从外部文件中将数据读到程序中来处理
    对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。
    这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:

    1. int a,b;  
    2. ifstream infile;  
    3. infile.open("myfile.txt");      //注意文件的路径  
    4. infile>>a>>b;                   //两行数据可以连续读出到变量里  
    5. infile.close()  
    6.   
    7. //如果是个很大的多行存储的文本型文件可以这么读:  
    8. char buf[1024];                //临时保存读取出来的文件内容  
    9. string message;  
    10. ifstream infile;  
    11. infile.open("myfile.<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' style='color:#df3434; font-weight:bold;'>js</a>");  
    12. if(infile.is_open())          //文件打开成功,说明曾经写入过东西  
    13. {  
    14. while(infile.good() && !infile.eof())  
    15. {  
    16.     memset(buf,0,1024);  
    17.     infile.getline(buf,1204);  
    18.     message = buf;  
    19.     ......                     //这里可能对message做一些操作  
    20.     cout<<message<<endl;  
    21. }  
    22. infile.close();  
    23. }  

    写:将程序中处理后的数据写到文件当中
    对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:

    1. ofstream outfile;  
    2. outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名  
    3. if(outfile.is_open())  
    4. {  
    5. outfile<<message<<endl;    //message是程序中处理的数据  
    6.    outfile.close();   
    7. }  
    8. else  
    9. {  
    10.    cout<<"不能打开文件!"<<endl;  
    11. }  

    c++对文件的读写操作的例子

    [c-sharp] view plain copy
      1. /*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序, 
      2. 将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/   
      3. #include<fstream>  
      4. #include<iostream>  
      5. #include<cmath>  
      6. using namespace std;  
      7. //////////////从键盘上读取字符的函数  
      8. void read_save(){  
      9.       char c[80];  
      10.       ofstream outfile("f1.dat");//以输出方工打开文件  
      11.       if(!outfile){  
      12.                    cerr<<"open error!"<<endl;//注意是用的是cerr   
      13.                    exit(1);  
      14.                    }  
      15.           cin.getline(c,80);//从键盘读入一行字符  
      16.           for(int i=0;c[i]!=0;i++) //对字符一个一个的处理,直到遇到'/0'为止   
      17.                 if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符是字符   
      18.                    outfile.put(c[i]);//将字母字符存入磁盘文件   
      19.                    cout<<c[i]<<"";  
      20.                    }  
      21.                    cout<<endl;  
      22.                    outfile.close();  
      23.                    }  
      24. void creat_data(){  
      25.       char ch;  
      26.       ifstream infile("f1.dat",<a href="http://lib.csdn.net/base/ios" class='replace_word' title="iOS知识库" target='_blank' style='color:#df3434; font-weight:bold;'>iOS</a>::in);//以输入的方式打开文件   
      27.       if(!infile){  
      28.                   cerr<<"open error!"<<endl;  
      29.                   exit(1);  
      30.                   }  
      31.     ofstream outfile("f3.dat");//定义输出流f3.dat文件  
      32.     if(!outfile){  
      33.                  cerr<<"open error!"<<endl;  
      34.                  exit(1);  
      35.                  }  
      36.      while(infile.get(ch)){//当读取字符成功时   
      37.      if(ch<=122&&ch>=97)  
      38.      ch=ch-32;  
      39.      outfile.put(ch);  
      40.      cout<<ch;  
      41.      }  
      42.      cout<<endl;  
      43.      infile.close();  
      44.      outfile.close();  
      45.      }  
      46.      int main(){  
      47.          read_save();  
      48.          creat_data();  
      49.         system("pause");  
      50.          return 0;  
      51.          }  
  • 相关阅读:
    SGU 495 Kids and Prizes 概率DP 或 数学推理
    poj 2799 IP Networks 模拟 位运算
    uva 202 Repeating Decimals 模拟
    poj 3158 Kickdown 字符串匹配?
    uva 1595 Symmetry 暴力
    uva 201 Squares 暴力
    uva 1594 Ducci Sequence 哈希
    uva 1368 DNA Consensus String 字符串
    数字、字符串、列表的常用操作
    if条件判断 流程控制
  • 原文地址:https://www.cnblogs.com/qiushui127/p/7339330.html
Copyright © 2011-2022 走看看