掌握文本文件读写的方法
了解二进制文件的读写方法
C++文件流:
fstream
ifstream
ofstream
#include<iomanip.h>
#include<fstream.h>
void
main()
{
ofstream f1(
"d:\\me.txt"
);
if
(!f1)
return
;
f1<<setw(20)<<
"姓名:"
<<
"廉东方"
<<endl;
f1<<setw(20)<<
"家庭地址:"
<<
"河南郑州"
<<endl;
f1.close();
}
运行后打开文件d:\me.txt,其内容如下:
姓名:廉东方
家庭地址:河南郑州
文件操作:
打开文件
文件名
注意路径名中的斜杠要双写,如:
"D:\\MyFiles\\ReadMe.txt"
文件打开方式选项:
ios::
in
= 0x01,
ios::
out
= 0x02,
ios::ate = 0x04,
ios::app = 0x08,
ios::trunc = 0x10,
ios::nocreate = 0x20,
ios::noreplace = 0x40,
ios::binary = 0x80
文件保护方式选择项:
filebuf::openprot;
filebuf::sh_none;
filebuf::sh_read;
filebuf::sh_write;
打开文件的方法
调用构造函数时指定文件名和打开模式
ifstream f(
"d:\\12.txt"
,ios::nocreate);
ofstream f(
"d:\\12.txt"
);
fstream f(
"d:\\12.dat"
,ios::
in
|ios::
out
|ios::binary);
使用Open成员函数
fstream f;
f.open(
"d:\\12.txt"
,ios::
out
);
检查是否成功打开
成功:
if
(f){...}
if
(f.good()){...}
失败:
if
(!f){...}
if
(f.fail()){...}
读写操作
使用<<,>>运算符
只能进行文本文件的读写操作,用于二进制文件可能会产生错误。
使用函数成员
get
、put、read、write等
经常和read配合使用的函数是gcount(),用来获得实际读取的字节数。
读写二进制文件注意事项
打开方式中必须指定ios::binary,否则读写会出错
用read\write进行读写操作,而不能使用插入、提取运算符进行操作,否则会出错。
使用eof()函数检测文件是否读结束,使用gcount()获得实际读取的字节数
关闭文件
使用成员函数close,如:
f.close();
利用析构函数
对象生命期结束时会检查文件是否关闭,对没有关闭的文件进行关闭操作。
随机读写文件
通过移动文件读写指针,可在文件指定位置进行读写。
seekg(绝对位置);
seekg(相对位置,参照位置);
tellg();
seekp(绝对位置);
seekp(相对位置,参照位置);
tellp();
参照位置:
ios::beg = 0
ios::cur = 1
ios::end = 2
读写文本文件的示例
#include<fstream.h>
void
main()
{
fstream f(
"d:\\try.txt"
,ios::
out
);
f<<1234<<
' '
<<3.14<<
'A'
<<
"How are you"
;
f.close();
f.open(
"d:\\try.txt"
,ios::
in
);
int
i;
double
d;
char
c;
char
s[20];
f>>i>>d>>c;
f.getline(s,20);
cout<<i<<endl;
cout<<d<<endl;
cout<<c<<endl;
cout<<s<<endl;
f.close();
}
运行结果:
1234
3.14
A
How are you
Press any key to
continue
显示文本文件的内容
#include<fstream.h>
void
main()
{
ifstream fin(
"d:\\简介.txt"
,ios::nocreate);
if
(!fin){
cout<<
"File open error!\n"
;
return
;
}
char
c;
while
((c=fin.
get
())!=EOF)cout<<c;
fin.close();
}
#include<fstream.h>
void
main()
{
ifstream fin(
"d:\\简介.txt"
,ios::nocreate);
if
(!fin){
cout<<
"File open error!\n"
;
return
;
}
char
c[80];
while
(fin.
get
(c,80,
'\0'
)!=NULL)cout<<c;
fin.close();
}
#include<fstream.h>
void
main()
{
ifstream fin(
"d:\\简介.txt"
,ios::nocreate);
if
(!fin){
cout<<
"File open error!\n"
;
return
;
}
char
c[80];
while
(!fin.eof())
{
fin.read(c,80);
cout.write(c,fin.gcount());
}
fin.close();
}
拷贝文件
#include<fstream.h>
void
main()
{
ifstream fin(
"C:\\1.exe"
,ios::nocreate|ios::binary);
if
(!fin){
cout<<
"File open error!\n"
;
return
;
}
ofstream fout(
"C:\\2.exe"
,ios::binary);
char
c[1024];
while
(!fin.eof())
{
fin.read(c,1024);
fout.write(c,fin.gcount());
}
fin.close();
fout.close();
cout<<
"Copy over!\n"
;
}
相关推荐
完整的,可以读取多种格式的文件 源代码。
linux C++ 文本文件创建写入以及读取
C++ 文件读写 C++ 中的文件读写是通过 stream 类及其子类 fstream 实现的。fstream 是 file stream 的缩写,表示文件流。它提供了成员函数 open() 用于打开文件,close() 用于关闭文件,并提供了插入器 () 和析取...
详细介绍了c++的文件读写操作以及使用技巧,文章通俗易懂,有实例。
OpenXLSX是一套开源的读、写、创建、修改Excel文件(格式:xlsx)的C++库。附件内容:源码+示例。
一个简单的C++读写文件的实现代码,供初学者参考。
熟悉对文件的读写操作,主要是C/C++,程序代码完整,可以编译运行。
C++读写文件文档介绍,介绍了C++从键盘读写的详细的函数
https://github.com/nlohmann/json 最好用的C++读写json库
1.读取文件,并根据文件的数字计算出总数 2.创建并读取文件并输出文件里的内容 3.String Analyser // The purpose of this exercise is to create basic objects and run functions on objects
c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件c++读取xml文件
linux c++csv文件读写,linux c++csv文件读写的代码的实现。
c++ /c read .ply 读取.ply文件 读取文件 PLY多边形文件格式
在原作者的基础上进行修改的,需要C请去https://download.csdn.net/download/taoyuanmin/9179573下载,或者再次修改即可
项目使用vs2015,使用的git上的三方开源框架,进行了裁剪,基于C++创建,读写parquet文件。
C++读写ini文件封装成类,直接创建对象对ini文件进行读写操作,真实有效 ,可以联系我 关注:https://blog.csdn.net/qq_34510308
C++文件读写操作C++文件读写操作 C++文件读写操作C++文件读写操作