c++對txt文件的讀取與寫入
阿新 • • 發佈:2017-12-31
lin 一個 離開 term file 例子 內容 存儲 turn
轉自: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 12if(!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
無論讀寫都要包含<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.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 }
c++對txt文件的讀取與寫入