C++_文件读写
来源:互联网 发布:linux 进程调度 编辑:程序博客网 时间:2024/05/21 00:47
相关的头文件:#include <fstream>
需要相关的类
fstream提供三种类,实现C++对文件的操作
ofstream:写操作,由ostream引申而来
ifstream:读操作,由istream引申而来
fstream :同时读写操作,由iostream引申而来
文件的类型:文本文件 和 二进制文件
文件读写的步骤:
1、包含的头文件:#include <fstream>
2、创建流
3、打开文件(文件和流关联)
4、读写 (写操作:<<,put( ), write( ) 读操作: >> , get( ),getline( ), read( ))
5、关闭文件:把缓冲区数据完整地写入文件, 添加文件结束标志, 切断流对象和外部文件的连接
文件的读写:
1、文本文件的读写:
方法:
一次性读写若干字符
1)使用运算符<< 和 >>进行读写
功能:
<< 能实现以行为单位写入文件
>> 不能一行为单位读入内存,总是以空格、Tab、回车结束,而是以单词为单位
代码:
函数功能:使用<< ,写入文件一行字符
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- ofstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile<<"abc def ghi";
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){ofstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile<<"abc def ghi";OpenFile.close();system("pause");}
运行结果:文件中写入内容:abc def ghi
函数功能:使用>>,从文件读入一个单词
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- const int len=20;
- char str[len];
- ifstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile>>str;
- cout<<str<<endl;
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){const int len=20;char str[len];ifstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile>>str;cout<<str<<endl;OpenFile.close();system("pause");}
运行结果:str的内容为abc,而不是abc def ghi(见空格停止)
2)使用运算符<<(写)和getline()进行读写
功能:
<<:以行为单位输入文件
getline():以行为单位 读入内存,能一次读入一行
函数原型:istream &getline( char *buffer, streamsize num );
功能:getline( )函数用于从文件读取num-1个字符到buffer(内存)中,直到下列情况发生时,读取结束:
1):num - 1个字符已经读入
2):碰到一个换行标志
3):碰到一个EOF
代码:
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- const int len=20;
- char str[len];
- ifstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile.getline(str,20);
- cout<<str<<endl;
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){const int len=20;char str[len];ifstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile.getline(str,20);cout<<str<<endl;OpenFile.close();system("pause");}
运行结果:str的内容为abc def ghi (一直把一行读完)
一次读写一个字符:
使用get( )和put( )函数
函数声明:
函数功能:使用 get( )函数 把字符1输入到文件
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch='1';
- ofstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile.put(ch);
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch='1';ofstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile.put(ch);OpenFile.close();system("pause");}
运行结果:把字符1写入文件
函数功能:使用 put( )函数 把文件中第一个字符输入内存
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch;
- ifstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile.get(ch);
- cout<<ch;
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch;ifstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile.get(ch);cout<<ch;OpenFile.close();system("pause");}
运行结果:把字符1从文件中读到ch(内存)中
2、二进制文件的读写:
1)使用运算符get( ) 和 put( )读写一个字节
功能:
get( ) :在文件中读取一个字节到内存
函数原型:ifstream &get(char ch)
put( ) :在内存中写入一个字节到文件
函数原型:ofstream &put(char ch)
代码:
功能:把26个字符写入文件中
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch='a';
- ofstream OpenFile("file.txt",ios::binary);
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- for (int i=0;i<26;i++)
- {
- OpenFile.put(ch);
- ch++;
- }
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch='a';ofstream OpenFile("file.txt",ios::binary);if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}for (int i=0;i<26;i++){OpenFile.put(ch);ch++;}OpenFile.close();system("pause");}
运行结果:文件内容为abcdefghijklmnopqlst...z
功能:把文件中的26个字母读入内存
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch;
- ifstream OpenFile("file.txt",ios::binary);
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- while (OpenFile.get(ch))
- cout<<ch;
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch;ifstream OpenFile("file.txt",ios::binary);if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}while (OpenFile.get(ch))cout<<ch;OpenFile.close();system("pause");}
运行结果:ch依次为abc...z
2)使用read()和write()进行读写
read( ):
功能:从文件中提取 n 个字节数据,写入buf指向的地方中
函数声明:istream & read ( char * buf , int n ) ;
代码:
函数功能:使用write( )函数,一次从内存向文件写入一行数据
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch[12]="12 3 456 78";
- ofstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile.write(ch,12);
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch[12]="12 3 456 78";ofstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile.write(ch,12);OpenFile.close();system("pause");}
运行结果:文件内容12 3 456 78
write( ):
功能:把buf指向的内容取n个字节写入文件
函数声明:ostream & ostream :: write ( char * buf , int n ) ;
参数说明:buf表示要写入内存的地址,传参时要取地址。n表示要读入字节的长度
注意:1):该函数遇到空字符时并不停止,因而能够写入完整的类结构
2):第一个参数一个char型指针(指向内存数据的起始地址),与对象结合使用的时候,要在对象地址之前要char做强制类型转换。
函数功能:使用write( )函数,一次从文件向内存写入一行数据
- #include <fstream>
- #include <iostream>
- using namespace std;
- void main()
- {
- char ch[12];
- ifstream OpenFile("file.txt");
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
- OpenFile.read(ch,12);
- cout<<ch;
- OpenFile.close();
- system("pause");
- }
#include <fstream>#include <iostream>using namespace std;void main(){char ch[12];ifstream OpenFile("file.txt");if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}OpenFile.read(ch,12);cout<<ch;OpenFile.close();system("pause");}
运行结果:数组ch的内容为12 3 456 78 。
说明:
1、程序不再使用文件时,为什么要关闭文件?
因为:1)文件缓冲区是一块小的内存空间.
注意:close ( ) 函数关闭文件,但流对象仍然存在。
2、
3、针对文本文件操作时,get函数和>>的区别:
- if (OpenFile)
- {
- cout<<"打开文件失败!";
- exit(0);
- }
- if (OpenFile.fail())
- {
- cout<<"打开文件错误!"<<endl;
- exit(0);
- }
if (OpenFile){cout<<"打开文件失败!";exit(0);}if (OpenFile.fail()){cout<<"打开文件错误!"<<endl;exit(0);}
- while (!OpenFile.eof())
- {
- //文件结束时的代码
- }
while (!OpenFile.eof()){//文件结束时的代码}
- while (!OpenFile)
- {
- //文件结束时的代码
- }
while (!OpenFile){ //文件结束时的代码}
- while ( (OpenFile.get(ch) )!=EOF)
- {
- //成功时候的代码
- }
while ( (OpenFile.get(ch) )!=EOF){//成功时候的代码}
文本文件的读写常使用的方法:使用<<写入文件,使用getline 和 >> 读到内存
二进制文件的读写常使用的方法:使用istream 类的成员函数read 和write 来实现,
这两个成员函数的原型为:
- istream& read(char *buffer,int len);
- ostream& write(const char * buffer,int len);
istream& read(char *buffer,int len);ostream& write(const char * buffer,int len);
参数说明:字符指针 buffer 指向内存中一段存储空间。len 是读/写的字节数。
与对象结合写入二进制文件时:
write函数调用语句:
- 输出文件流对象名.write((char*)& 对象名,sizeof(<对象所属类名>));
- 输出文件流对象名.write((char*)& 对象数组名[下标],sizeof(<对象所属类名>));
输出文件流对象名.write((char*)& 对象名,sizeof(<对象所属类名>));输出文件流对象名.write((char*)& 对象数组名[下标],sizeof(<对象所属类名>));
read函数调用语句:
- 输入文件流对象名.read((char*)& 对象名,sizeof(<对象所属类名>));
- 输入文件流对象名.read((char*)& 对象数组名[下标],sizeof(<对象所属类名>));
输入文件流对象名.read((char*)& 对象名,sizeof(<对象所属类名>)); 输入文件流对象名.read((char*)& 对象数组名[下标],sizeof(<对象所属类名>));
注意:gcount()函数经常和read函数配合使用,用来获得实际读取的字节数。
- C++_文件读写
- C++_文件读写
- C#_文件读写常用类介绍
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- 【C/C++】文件读写
- Java_IO流_读写文件
- Android开发_读写文件
- 19_文件的读写
- c#_xml读写_操作_实例
- c语言读写文件
- 文件读写(c)
- C#-读写INI文件
- [C++]文件读写操作
- C#读写INI文件
- C 读写文件
- C读写ini文件
- c#FileStream文件读写
- 读写文件 c
- 软软解析之缓存游标的迷惑-----已解决!
- python - Local variable referenced before assignment
- C++_模板举例_使用模板实现集合类(堆栈)
- android R.java 文件丢失
- cocos2d-x-教程 让精灵响应触摸 并把方向旋转到相对应的角度
- C++_文件读写
- LDD3源码学习日记<七>
- route命令
- hdu-2161-Primes
- C++_文本文件读写常用代码
- ACM 母函数模板 详解 + 理解!(母函数二)
- C++_输入常用函数
- Junit(单元测试)的使用
- ping命令