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   
44  
45  
46   
47 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dragonworrior/archive/2009/11/02/4759484.aspx

无论读写都要包含头文件

读:从外部文件中将数据读到程序中来处理

对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象: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.js");  
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++对文件的读写操作的例子

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          }

原文链接: https://www.cnblogs.com/zhengfa-af/p/8157235.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/266898

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午6:06
下一篇 2023年2月14日 下午6:09

相关推荐