看流星社区

 找回密码
 注册账号
查看: 1588|回复: 0

c++对txt文件的读取与写入

[复制链接]

该用户从未签到

发表于 2014-6-16 08:54:39 | 显示全部楼层 |阅读模式
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>

  4. using namespace std;

  5. int main(){
  6. char buffer[256];
  7. ifstream myfile ("c://a.txt");
  8. ofstream outfile("c://b.txt");

  9. if(!myfile){
  10.   cout << "Unable to open myfile";
  11.         exit(1); // terminate with error

  12. }
  13. if(!outfile){
  14.     cout << "Unable to open otfile";
  15.         exit(1); // terminate with error

  16. }
  17. int a,b;
  18. int i=0,j=0;
  19. int data[6][2];
  20.   while (! myfile.eof() )
  21.   {
  22.      myfile.getline (buffer,10);
  23.     sscanf(buffer,"%d %d",&a,&b);
  24.     cout<<a<<" "<<b<<endl;
  25.      data[i][0]=a;
  26.      data[i][1]=b;
  27.      i++;
  28.   }
  29. myfile.close();
  30.   for(int k=0;k<i;k++){
  31.       outfile<<data[k][0] <<" "<<data[k][1]<<endl;
  32.      cout<<data[k][0] <<" "<<data[k][1]<<endl;
  33.   }

  34. outfile.close();
  35. return 0;
  36. }
复制代码
无论读写都要包含<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. char buf[1024];                //临时保存读取出来的文件内容
  8. string message;
  9. ifstream infile;
  10. infile.open("myfile.js");
  11. if(infile.is_open())          //文件打开成功,说明曾经写入过东西
  12. {
  13. while(infile.good() && !infile.eof())
  14. {
  15.     memset(buf,0,1024);
  16.     infile.getline(buf,1204);
  17.     message = buf;
  18.     ......                     //这里可能对message做一些操作
  19.     cout<<message<<endl;
  20. }
  21. infile.close();
  22. }

复制代码
写:将程序中处理后的数据写到文件当中
对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象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++对文件的读写操作的例子
  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",ios::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.          }
复制代码
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-5-29 06:51

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表