-
#include <fstream>
-
-
-
-
#include<iomanip.h>
-
#include<fstream>
-
-
void
main()
-
{
-
ofstream ofs(
"C:\\example.txt"
);
-
if
(!ofs)
return
;
-
-
f1 << setw(20) <<
"Name: "
<<
"Beethoven"
<< endl;
-
f1 << setw(20) <<
"song: "
<<
"Moonlight Sonata"
<< endl;
-
f1.close();
-
}
文件操作:
打开文件
文件名
注意路径名中的斜杠要双写,如:
"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
文件保护方式选择项:
[cpp]
view plain
-
filebuf::openprot;
-
filebuf::sh_none;
-
filebuf::sh_read;
-
filebuf::sh_write;
打开文件的方法
调用构造函数时指定文件名和打开模式
[cpp]
view plain
-
ifstream f(
"d:\\12.txt"
, ios::nocreate);
-
ofstream f(
"d:\\12.txt"
);
-
fstream f(
"d:\\12.dat"
, ios::in|ios::out|ios::binary);
使用Open成员函数
[cpp]
view plain
-
if
(f) {...}
-
if
(f.good()) {...}
[cpp]
view plain
读写操作
使 用<<,>>运算符
只能进行文本文件的读写操作,用于二进制文件可能会产生错误。
使用函数成员 get、put、read、write等
经常和read配合使用的函数是 gcount(),用来获得实际读取的字节数。
读写二进制文件注意事项
打开方式中必须指定
iOS
::binary,否则读写会出错
用read\write进行读写操作,而不能使用插入、提取运算符进行操作,否则 会出错。
使用eof()函数检测文件是否读结束,使用gcount()获得实际读取的字节数
关闭文件
使用成员函数close, 如:
oracle
f.close();
利用析构函数
对象生命期结 束时会检查文件是否关闭,对没有关闭的文件进行关闭操作。
随机读写文件
通过移动文件读写指针,可在文件指定位置进行读写。
[cpp]
view plain
-
seekg(绝对位置);
-
seekg(相对位置,参照位置);
-
tellg();
-
seekp(绝对位置);
-
seekp(相对位置,参照位置);
-
tellp();
参照位置:
MySQL
[html]
view plain
-
ios::beg
=
0
//相对于文件头
-
ios::cur
=
1
//相对于当前位置
-
ios::end
=
2
//相对于文件尾
写文本文件的示例
//为能够正确读出写入文件的各数据,各数据间最好要有分隔
-
#include
<
fstream
>
-
-
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
显示文本文件的内容
-
//使用get()一次读一个字符--------------------------------方案一
-
#include
<
fstream
>
-
-
-
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();
-
}
//使用get(char *,int n,char delim='\n')一次读多个字符----方案二
//巧妙利用文本文件中不会有字符'\0'的特点进行读取
[cpp]
view plain
-
#include<fstream>
-
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();
-
}
拷贝文件
//二进制文件操作示例
ssh
[cpp]
view plain
-
#include<fstream>
-
-
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"
;
-
}
一个打开并检查输入文件的程序:
[cpp]
view plain
-
#include<iostream>
-
#include<fstream>
-
#include<string>
-
using
namespace
std;
-
ifstream& open_file(ifstream &in,
const
string &file)
-
{
-
in.close();
-
in.clear();
-
in.open(file.c_str());
-
return
in;
-
}
-
int
main()
-
{
-
ifstream file;
-
open_file(file,
"1.txt"
);
-
string s;
-
while
(getline(file,s))
-
{
-
cout<<s<<endl;
-
}
-
file.close();
-
return
0;
-
}
c++ fstream 获取文件大小
c++ 判断文件是否空
c++ 判断文件是否存在
-
ios::_nocreate = 0x20,
-
ios::_noreplace = 0x40,
利用上述标志来打开文件,看其是否打开成功,不成功则说明其不存在。
c++ 只能间接通过
打开文件来
判断文件是否存在?
C++中文件流(fstream)的使用方法及示例C++文件流:[cpp] view plain copyfstream // 文件流 ifstream // 输入文件流 ofstream // 输出文件流 [cpp] view plain copy
最近在写一个工具,需要去分段读取日志,由于日志很大(可能10GB+),所以不能一次读到内存
中
我发现,当
使用
"r"/“ios::in”去读取
文件
的时候,读取完之后,
文件
指针的位置会比read的buffer要靠后
比如以下代码
inLogFile.open(file, ios::in);
unsigned long long blockSize = 16 * 1024 * 1024;//16MB
char *tempStr = new char[blockSize];
inLogFile.read(tempS
using namespace std;
void Trim(string &str){ // 去掉字符串首尾的特殊字符 \f:换页 \v:垂直制表 \r:回车 \t:水平制表 \n:换行
string blanks("\f\v\r\t\n.
2、参数: filename 操作
文件
名 mode 打开
文件
的方式 prot 打开
文件
的属性 //基本很少用到,在查看资料时,发现有两种方式打开
文件
的方式在ios类(所以
流
式I/O的基类)
中
定义,有如下几种方式:
这些方式是能够进行组合
使用
的,以“或”运算(“|”)的方式:例如
很多程序
中
,可能会碰到o
fstream
ou