轉(C++操作文件)

 C++讀取文件txt,循環逐行輸出(轉)
筆記:C++文件的讀取和寫入


#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main(){
char buffer[256];
ifstream myfile ("c:\\a.txt");
ofstream outfile("c:\\b.txt");

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

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

}
int a,b;
int i=0,j=0;
int data[6][2];
  while (! myfile.eof() )
  {
     myfile.getline (buffer,10);
    sscanf(buffer,"%d %d",&a,&b);
    cout<<a<<" "<<b<<endl;
     data[i][0]=a;
     data[i][1]=b;
     i++;
  }
myfile.close();
  for(int k=0;k<i;k++){
      outfile<<data[k][0] <<" "<<data[k][1]<<endl;
     cout<<data[k][0] <<" "<<data[k][1]<<endl;
  }

outfile.close();
return 0;
}

無論讀寫都要包含<fstream>頭文件

讀:從外部文件中將數據讀到程序中來處理
對於程序來說,是從外部讀入數據,因此定義輸入流,即定義輸入流對象:ifsteam infile,infile就是輸入流對象。
這個對象當中存放即將從文件讀入的數據流。假設有名字爲myfile.txt的文件,存有兩行數字數據,具體方法:
int a,b;
ifstream infile;
infile.open("myfile.txt");      //注意文件的路徑
infile>>a>>b;                   //兩行數據可以連續讀出到變量裏
infile.close()

如果是個很大的多行存儲的文本型文件可以這麼讀:
char buf[1024];                //臨時保存讀取出來的文件內容
string message;
ifstream infile;
infile.open("myfile.js");
if(infile.is_open())          //文件打開成功,說明曾經寫入過東西
{
while(infile.good() && !infile.eof())
{
    memset(buf,0,1024);
    infile.getline(buf,1204);
    message = buf;
    ......                     //這裏可能對message做一些操作
    cout<<message<<endl;
}
infile.close();
}

寫:將程序中處理後的數據寫到文件當中
對程序來說是將數據寫出去,即數據離開程序,因此定義輸出流對象ofstream outfile,outfile就是輸出流對象,這個對象用來存放將要寫到文件當中的數據。具體做法:
ofstream outfile;
outfile.open("myfile.bat"); //myfile.bat是存放數據的文件名
if(outfile.is_open())
{
outfile<<message<<endl;    //message是程序中處理的數據
   outfile.close();
}
else
{
   cout<<"不能打開文件!"<<endl;
}


c++對文件的讀寫操作的例子

/*/從鍵盤讀入一行字符,把其中的字母依次放在磁盤文件fa2.dat中,再把它從磁盤文件讀入程序,
將其中的小寫字母改成大寫字母,再存入磁盤fa3.dat中*/
#i nclude<fstream>
#i nclude<iostream>
#i nclude<cmath>
using namespace std;
//////////////從鍵盤上讀取字符的函數
void read_save(){
      char c[80];
      ofstream outfile("f1.dat");//以輸出方工打開文件
      if(!outfile){
                   cerr<<"open error!"<<endl;//注意是用的是cerr
                   exit(1);
                   }
          cin.getline(c,80);//從鍵盤讀入一行字符
          for(int i=0;c[i]!=0;i++) //對字符一個一個的處理,直到遇到'/0'爲止
                if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保證輸入的字符是字符
                   outfile.put(c[i]);//將字母字符存入磁盤文件
                   cout<<c[i]<<"";
                   }
                   cout<<endl;
                   outfile.close();
                   }
void creat_data(){
      char ch;
      ifstream infile("f1.dat",ios::in);//以輸入的方式打開文件
      if(!infile){
                  cerr<<"open error!"<<endl;
                  exit(1);
                  }
    ofstream outfile("f3.dat");//定義輸出流f3.dat文件
    if(!outfile){
                 cerr<<"open error!"<<endl;
                 exit(1);
                 }
     while(infile.get(ch)){//當讀取字符成功時
     if(ch<=122&&ch>=97)
     ch=ch-32;
     outfile.put(ch);
     cout<<ch;
     }
     cout<<endl;
     infile.close();
     outfile.close();
     }
     int main(){
         read_save();
         creat_data();
        system("pause");
         return 0;
         }
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章