fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。
fstream - 打开文件供读写
文件打开模式:
ios::in 读
ios::out д
ios::app 从文件末尾开始写
ios::binary 二进制模式
ios::nocreate 打开一个文件时,如果文件不存在,不创建文件。
ios::noreplace 打开一个文件时,如果文件不存在,创建该文件
ios::trunc 打开一个文件,然后清空内容
ios::ate 打开一个文件时,将位置移动到文件尾
文件指针位置在c++中的用法:
ios::beg 文件头
ios::end 文件尾
ios::cur 当前位置
例子:
file.seekg(0,ios::beg); //让文件指针定位到文件开头
file.seekg(0,ios::end); //让文件指针定位到文件末尾
file.seekg(10,ios::cur); //让文件指针从当前位置向文件末方向移动10个字节
file.seekg(-10,ios::cur); //让文件指针从当前位置向文件开始方向移动10个字节
file.seekg(10,ios::beg); //让文件指针定位到离文件开头10个字节的位置
常用的错误判断方法:
good() 如果文件打开成功
bad() 打开文件时发生错误
eof() 到达文件尾
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ofstream inFile; /*ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建*/ inFile.open("inFile.txt",ios::trunc); int i; char a='a'; for(i=1;i<=26;i++)//将26个数字及英文字母写入文件 { inFile<<setw(2)<<i<<"\t"<<a<<"\n"; a++; } inFile.close();//关闭文件 } /************************************** inFile 内容 如下 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n 15 o 16 p 17 q 18 r 19 s 20 t 21 u 22 v 23 w 24 x 25 y 26 z ***************************************/
#include <iostream> #include <fstream> using namespace std; void testByChar() { fstream testByCharFile; char c; testByCharFile.open("inFile.txt",ios::in); while(!testByCharFile.eof()) { testByCharFile>>c; cout<<c; } testByCharFile.close(); } void testByLine() { char buffer[256]; fstream outFile; outFile.open("inFile.txt",ios::in); cout<<"inFile.txt"<<"--- all file is as follows:---"<<endl; while(!outFile.eof()) { outFile.getline(buffer,256,'\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束 cout<<buffer<<endl; } outFile.close(); } int main() { cout<<endl<<"逐个字符的读取文件:testByChar() "<<endl<<endl; testByChar(); cout<<endl<<"将文件每行内容存储到字符串中,再输出字符串 :testByLine()"<<endl<<endl; testByLine(); } /********************** 运行结果 逐个字符的读取文件:testByChar() 1a2b3c4d5e6f7g8h9i10j11k12l13m14n15o16p17q18r19s20t21u22v23w24x25y26zz 将文件每行内容存储到字符串中,再输出字符串 :testByLine() inFile.txt--- all file is as follows:--- 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n 15 o 16 p 17 q 18 r 19 s 20 t 21 u 22 v 23 w 24 x 25 y 26 z Process returned 0 (0x0) execution time : 0.484 s Press any key to continue. *************************************************/
//如何统计文本的行数及如何读取文件某一行内容: #include <iostream> #include <fstream> #include <string> using namespace std; int CountLines(char *filename) { ifstream ReadFile; int n=0; string tmp; ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件 if(ReadFile.fail())//文件打开失败:返回0 { return 0; } else//文件存在 { while(getline(ReadFile,tmp,'\n')) { n++; } ReadFile.close(); return n; } } string ReadLine(char *filename,int line) { int lines,i=0; string temp; fstream file; file.open(filename,ios::in); lines=CountLines(filename); if(line<=0) { return "Error 1: 行数错误,不能为0或负数。"; } if(file.fail()) { return "Error 2: 文件不存在。"; } if(line>lines) { return "Error 3: 行数超出文件长度。"; } while(getline(file,temp)&&i<line-1) { i++; } file.close(); return temp; } int main() { int line; char filename[]="inFile.txt"; cout<<"该文件行数为:"<<CountLines(filename)<<endl; cout<<"\n请输入要读取的行数:"<<endl; while(cin>>line) { cout<<"第"<<line<<"行的内容是 :"<<endl; cout<<ReadLine(filename,line); cout<<"\n\n请输入要读取的行数:"<<endl; } } /********************************** 程序运行情况如下: 该文件行数为:26 请输入要读取的行数: -3 第-3行的内容是 : Error 1: 行数错误,不能为0或负数。 请输入要读取的行数: 4 第4行的内容是 : 4 d 请输入要读取的行数: 8 第8行的内容是 : 8 h 请输入要读取的行数: 26 第26行的内容是 : 26 z 请输入要读取的行数: 33 第33行的内容是 : Error 3: 行数超出文件长度。 请输入要读取的行数: 66 第66行的内容是 : Error 3: 行数超出文件长度。 请输入要读取的行数: ^Z Process returned 0 (0x0) execution time : 24.632 s Press any key to continue. **********************************/
//读取文件数据到临时数组 #include <iostream> #include <fstream> #include <string> using namespace std; int CountLines(char *filename) { ifstream ReadFile; int n=0; string tmp; ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件 if(ReadFile.fail())//文件打开失败:返回0 { return 0; } else//文件存在 { while(getline(ReadFile,tmp,'\n')) { n++; } ReadFile.close(); return n; } } int main() { ifstream file; int LINES; char filename[512]="inFile.txt"; file.open(filename,ios::in); if(file.fail()) { cout<<"文件不存在."<<endl; file.close(); } else//文件存在 { LINES=CountLines(filename); int *tempInt=new int[LINES]; char *tempChar=new char[LINES]; int i=0; while(!file.eof()) //读取数据到数组 { file>>tempInt[i]; file>>tempChar[i]; i++; } file.close(); //关闭文件 for(i=0;i<LINES;i++)//输出数组内容 cout<<tempInt[i]<<"\t"<<tempChar[i]<<endl; delete []tempInt; delete []tempChar; } }
文章来源: c++写入