文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件

时间:2023-03-09 07:07:49
文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin  iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件


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表示有读写的权限,通过fstream.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这个文件的最后面多了:天下英雄,谭胜第一