c++文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.s…。
1.ofstream,open,close 写入文件
#include <iostream>
#include <fstream>
using namespace std ;
// 通过 ofstream 的方式实现写入文件 open,close
void main ()
{
ofstream fout ; //ofstream 输出文件
fout . open ( “E:\\1.txt” ); // 打开文件
fout << “1234abcdef” ; // 写入文件
fout . close ();
}
通过这些代码向文件 1.txt 中输入文件
2.ifstream,fin 从文件中读取文件并打印输出到屏幕
#include <iostream>
#include <fstream>
using namespace std ;
// 通过 ifstream 流读取文件,并将文件写入 str 中
void main ()
{
ifstream fin ( “E:\\1.txt” ); // 创建读取文件的流
char str [50] = { 0 };
fin >> str ; // 读取
fin . close ();
cout << str ;
cin . get ();
}
运行结果是,输出: 1234abcdef
3. 按照行来读取数据
#include <iostream>
#include <fstream>
using namespace std ;
// 按照行来读取
void main ()
{
// 按照行来读取
ifstream fin ( “E:\\1.txt” );
// 读取 4 行数据
for ( int i = 0; i < 4; i ++)
{
char str [50] = { 0 };
fin . getline ( str , 50);
cout << str << endl ;
}
fin . close ();
cin . get ();
}
上面结果是输出 4 行。
4.fout 文件输入
#include <iostream>
#include <fstream>
using namespace std ;
void main ()
{
ofstream fout ; //ofstream. 输出文件
fout . open ( “E:\\2.txt” ); // 打开文件
fout << ” 锄禾日当午 ” << endl ; // 写入文件
fout << ” 地雷买下土 ” << endl ; // 写入文件
fout << ” 谭胜来跳舞 ” << endl ; // 写入文件
fout << ” 炸成 250″ << endl ; // 写入文件
fout . close ();
}
5. fstream , ios::in | ios::out 表示有读写的权限,通过 f stream .getline( 写入位置,写入长度 ) 。
案例(写入文件又读取文件的方式):
#include <iostream>
#include <fstream>
using namespace std ;
// 通过 fstream 的方式实现文件读写,要注意的是这种方式要求文件已经存在
void main ()
{
//ios::in | ios::out 表示有读写的权限
fstream fio ( “E:\\3.txt” , ios :: in | ios :: out );
fio << ” 锄禾日当午 ” << endl ; // 写入文件
fio << ” 地雷买下土 ” << endl ; // 写入文件
fio << ” 谭胜来跳舞 ” << endl ; // 写入文件
fio << ” 炸成 250″ << endl ; // 写入文件
fio . close ();
{
fstream fio ( “E:\\3.txt” , ios :: in | ios :: out );
for ( int i = 0; i < 4; i ++)
{
char str [50] = { 0 };
fio . getline ( str , 50);
cout << str << endl ;
}
fio . close ();
}
cin . get ();
}
6.fio.seekg(); 随机文件指针,将文件指针移动到指定位置开始读写文件
案例如下:
#include <iostream>
#include <fstream>
using namespace std ;
// 通过 fstream 的方式实现文件读写,要注意的是这种方式要求文件已经存在
void main ()
{
//ios::in | ios::out 表示有读写的权限
fstream fio ( “E:\\3.txt” , ios :: in | ios :: out );
fio << ” 锄禾日当午 ” << endl ; // 写入文件
fio << ” 地雷买下土 ” << endl ; // 写入文件
fio << ” 谭胜来跳舞 ” << endl ; // 写入文件
fio << ” 炸成 250″ << endl ; // 写入文件
fio . close ();
fio . seekg ( ios :: beg ); // 文件指针,从文件开头开始执行
{
fstream fio ( “E:\\3.txt” , ios :: in | ios :: out );
for ( int i = 0; i < 4; i ++)
{
char str [50] = { 0 };
fio . getline ( str , 50);
cout << str << endl ;
}
fio . close ();
}
cin . get ();
}
7. 文件写入和文件读写
#include <iostream>
#include <fstream>
using namespace std ;
void main ()
{
ofstream fout ;
fout . open ( “E:\\4.txt” );
// 将内容打印到文件
fout << “ABC” << ” ” << 123 << ” ” << ‘ch’ << endl ;
fout . close ();
ifstream fin ( “E:\\4.txt” );
char str [10] = { 0 }; // 读取字符串
int num = 0;
char ch = ‘\0′ ;
fin >> str >> num >> ch ;
std :: cout << str << “\n” << num << “\n” << ch ;
std :: cin . get ();
}
运行结果:
ABC
123
8. 文件拷贝
#include <iostream>
#include <fstream>
using namespace std ;
// 读写一个字符
// 文本与二进制存储
void main ()
{
ifstream fin ( “E:\\4.txt” ); // 创建读取文件的流
ofstream fout ( “E:\\40.txt” );
if (! fin || ! fout )
{
std :: cout << ” 文件打开失败 ” ;
return ;
}
std :: cout << ” 文件拷贝开始 \n” ;
char ch = 0;
// 引用的方法读取到一个字符
while ( fout && fin . get ( ch ))
{
fout . put ( ch ); // 写入一个字节
}
fin . close ();
fout . close ();
std :: cout << ” 文件拷贝完成 ” ;
cin . get ();
}
运行结果:生成了一个 40.txt, 里面存储的是 4.txt 中的内容
9. 文件追加 ios:app
#include <iostream>
#include <fstream>
using namespace std ;
void main ()
{
ofstream fout ( “E:\\40.txt” , ios :: app ); // 追加
fout << ” 天下英雄,谭胜第一 \n” ;
fout . close ();
cin . get ();
}
发现在 40.txt 这个文件的最后面多了: 天下英雄,谭胜第一