C++文件操作

来源:互联网 发布:网络视听许可证 youku 编辑:程序博客网 时间:2024/05/29 06:35

1、头文件介绍

#include <iostream>//标准输入输出流
#include <fstream>//派生自iostream,包括ifstream和ofstream

using namespace std;//都在名称空间std中,别忘了加上

2、打开文件

const char* fileName="1.txt";//要打开的文件名,可以使路径名,默认情况下是所建工程下

fstream类派生了两个类ifstream\ofstream

fstream f(fileName,参数二);

参数二有多种形式,这里只看主要用的几种:

ios_base::in//打开文件用于读

ios_base::out//打开文件用于写

ios_base::app//打开文件,用于追加(不覆盖原有文件,默认情况是覆盖)

ios_base::binary//以二进制方式打开文件,默认情况下是文本文件方式

例:

fstream i(fileName,ios_base::in|ios_base::out);//打开文件用于读写(既可读也可写)

ifstream in(fileName,ios_base::binary|ios_base::in);//以二进制方式打开文件用于读

ofstream out(fileName,ios_base::out|ios_base::app);//打开文件用于追加

3、由于派生自iostream,很多其他的方法和iostream一样

比如:seekg()\eof()\clear()……

4、一些文件操作的例子

读写二进制文件

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include <string>  
  5. using namespace std;  
  6. struct plant  
  7. {  
  8.     char name[20];  
  9.     int age;  
  10.     char num[10];  
  11. };  
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.            plant p;  
  15.     p.name[20]=(char)"yao";  
  16.     p.age=21;  
  17.     p.num[10]=(char)"0950420011";  
  18.     ofstream pf("data.dat",ios_base::out|ios_base::app|ios_base::binary);  
  19.     pf.write((char*)&p,sizeof(p));  
  20.     pf.close();  
  21.     ifstream in("data.dat",ios_base::in|ios_base::binary);  
  22.     plant p1;  
  23.     in.read((char*)&p1,sizeof(p1));  
  24.     in.close();  
  25.     cout<<p1.age<<endl<<p1.name<<endl<<p1.num<<endl;  
  26.            return 0;  
  27. }  

循环读取指定文件的指定行

 

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <fstream>  
  4. #include <string>  
  5. #include <sstream>  
  6. #include <time.h>  
  7. //#include "iomanip.h"  
  8. //const char* file="data.dat"  
  9. using namespace std;  
  10. const int MAX=1024;  
  11. const int M=40;  
  12. //#define   
  13. //////======自定义函数判断输入是否为数字=======///////////  
  14. bool IsDigital(const string &str)  
  15. {  
  16.     //int flag=0;//字符串中数字的个数  
  17.     if(str=="")  
  18.         return false;  
  19.     const char* p=str.c_str();  
  20.     int length=str.length();  
  21.     for(int i=0;i<length;i++,p++)  
  22.     {  
  23.         if((*p<='0')||(*p>='9'))  
  24.             return false;  
  25.     }  
  26.     return true;  
  27. }  
  28.   
  29.   
  30.       
  31.       
  32.   
  33. int _tmain(int argc, _TCHAR* argv[])  
  34. {  
[cpp] view plain copy
  1.       char c;     
  2.       do  
  3.       {  
  4. fstream f;  
  5. cout<<"请输入文件名称(注:先把你要打开的文件复制到本程序下,如:'file.txt')"<<endl;  
  6. int flag=0;  
[cpp] view plain copy
  1.     do  
  2.     {  
  3.         string str;  
  4.         if(flag)  
  5.         {  
  6.             f.clear();  
  7.             cout<<"输入文件不存在,请重新输入:"<<endl;  
  8.         }  
  9.         cin>>str;  
  10.         const char* file=str.c_str();  
  11.         //string text="";  
  12.         f.open(file,ios_base::in|ios_base::out|ios_base::binary);  
  13.         flag++;  
  14.     }  
  15.     while(!f.is_open());  
  16.     int lineCount=1;//记录当前文件的总行数  
  17.     int SumByte=0;//记录文件的总大小  
  18.     while(!f.eof())  
  19.     {  
  20.         char c;  
  21.         c=f.get();  
  22.         if(c=='\n')  
  23.             lineCount++;  
  24.         SumByte++;  
  25.     }  
  26.     f.clear();  
  27.     float kb;  
  28.     kb=(float)SumByte/1024;  
  29.     cout<<"当前文件的字节数是:"<<SumByte<<" Bit"<<endl;  
  30.     cout<<"当前文件的总大小是:"<<kb<<" KB"<<endl;  
  31.     cout<<"当前文件的总行数是: "<<lineCount<<endl;  
  32.     cout<<"/*===================================================*/\n/*===================================================*/"<<endl;  
  33.   
  34.     int line;  
  35.     string str_line;  
  36.     cout<<"\n接下来请输入你要读取文件的行数:\n";  
  37.     cin>>str_line;  
  38.     while(!IsDigital(str_line))  
  39.     {  
  40.         cout<<"输入格式不正确,请重新输入:\n";  
  41.         cin>>str_line;  
  42.     }  
  43.     line=atoi(str_line.c_str());  
  44.   
  45.     cout<<"当前的line值 :"<<line<<endl;  
  46.     while((line<=0)||(line>lineCount))  
  47.     {  
  48.         cout<<"输入数据有误(1--"<<lineCount<<"间),请重新输入:\n";  
  49.         cin>>line;  
  50.     }  
  51.   
  52.     cout<<"你选择了输出第"<<line<<"行"<<endl;  
  53.     //line=cin.get();  
  54.     //string strOut;//输出内容  
  55.     cout<<"/*===================================================*/\n/*===================================================*/"<<endl;  
  56.     int count=0;  
  57.     char ch;  
  58.     f.seekg(0);//指向文件开始位置  
  59.     for(int i=1;i<line;i++)  
  60.     {  
  61.         /*do 
  62.         { 
  63.             //f.read((char*)&ch,sizeof ch); 
  64.             ch=f.get(); 
  65.             count++; 
  66.              
  67.         } 
  68.         while(ch!='\n');*/  
  69.         while(f.read((char*)&ch,sizeof ch))  
  70.         {  
  71.             count++;  
  72.             if(ch=='\n')  
  73.                 break;  
  74.         }  
  75.     }  
  76.     cout<<"输出count的值:"<<endl;  
  77.     cout<<count<<endl;  
  78.     f.seekg(count);//跳转到所选行数的开头  
  79.     cout<<"/*===================================================*/\n"<<endl;  
  80.     cout<<"\n接下来输出你所选择的行:\n";  
  81.     cout<<"/*===================================================*/\n"<<endl;  
  82.     char a;  
  83.     while(f.read((char*)&a,sizeof a))  
  84.     {  
  85.         cout<<a;  
  86.         if(a=='\n')  
  87.             break;  
  88.     }  
  89.     cout<<"////////////////////////////////////////////////////////////////"<<endl;  
  90.     cout<<"是否继续打开文件?是-选择y:否-按任意键结束(除y)"<<endl;  
  91.     cin>>c;  
  92. }  
  93. while(c=='y');  
  94.    // char p;  
  95.     //cin>>p;  
  96.     return 0;  
  97. }  

复制文件

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <fstream>  
  4. using namespace std;  
  5. const char* file1="1.txt";  
  6. const char* file2="2.txt";  
  7. //把文件1的内容写到文件2的末尾  
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     ifstream in(file1);//默认方式打开文件1进行读  
  11.     ofstream out(file2,ios_base::out|ios_base::app);//打开文件2进行写入(app为追加)  
  12.     out<<in.rdbuf();//将文件1的内容输入到文件2的流中  
  13.     in.close();//关闭文件流  
  14.     out.close();  
  15.     return 0;  
  16. }  


C++文件操作就介绍到这里

0 0