天津seo网站排名优化公司,中国十大公司排行榜,移动端网站建设的意义,简历模板免费网站1.C文件操作 C中文件操作头文件:fstream。 文件类型#xff1a;文件文件和二进制文件。 文件操作三大类#xff1a; ofstream 写操作 ifstream 读操作 fstream:读写操作 文件打开方式#xff1a;
标志说明ios::in只读ios::out只写,文件不存在则…1.C文件操作 C中文件操作头文件:fstream。 文件类型文件文件和二进制文件。 文件操作三大类 ofstream 写操作 ifstream 读操作 fstream:读写操作 文件打开方式
标志说明ios::in只读ios::out只写,文件不存在则创建存在则打开并截断原内容ios::ate打开一个已有的文件并指向文件读指针指向文件尾若文件不存在则打开出错ios::app打开文件从文件尾添加内容若文件不存在则创建ios::trunc打开文件同时会截断原内容单独使用时与ios::out相同ios::binary以二进制方式打开ios::in|ios::out打开文件可读也可写文件打开时原内容保持不变若不存在则打开出错ios::in|ios::out|ios::trunc打开文件可读写会截断原内容文件不存在则创建
2.文本方式写入示例
#include iostream
#include fstream
using namespace std;
int main()
{/*1.创建文件*/ofstream fp;fp.open(test.txt,ios::out);//创建文件会截断原内容if (!fp.is_open())//文件打开失败返回false{cout 文件打开失败! endl;return 0;}fp C文件操作示例! endl;fp 写入数据测试 endl;fp 姓名:IT_阿水 t工作方向: 嵌入式开发 t工作时间: 6年 endl;fp.close();//关闭文件system(pause);
}3.文本方式读取示例 C中读取数据有多种方式实现。
2.1 示例1重载读取
#include iostream
#include fstream
using namespace std;
int main()
{ifstream ifs;ifs.open(test.txt,ios::in);//只读方式打开if (!ifs.is_open()){cout 文件打开失败! endl;return 0;}string str;while (ifs str)//以字符串方式读取{cout str str endl;;}//关闭文件ifs.close();system(pause);
}2.2 利用成员函数getline读取
#include iostream
#include fstream
using namespace std;
int main()
{ifstream ifs;ifs.open(test.txt,ios::in);//只读方式打开if (!ifs.is_open()){cout 文件打开失败! endl;return 0;}//第二种:getline()char buff[1024];while (ifs.getline(buff, sizeof(buff))){cout buff buff endl;}//关闭文件ifs.close();system(pause);
}2.3 单个字符方式读取get()
#include iostream
#include fstream
using namespace std;
int main()
{ifstream ifs;ifs.open(test.txt,ios::in);//只读方式打开if (!ifs.is_open()){cout 文件打开失败! endl;return 0;}//第三种单个字符方式读取char c;while ((c ifs.get()) ! EOF){cout c;}//关闭文件ifs.close();system(pause);
}4.二进制方式读写示例 二进制数据写入文件
函数write(const _Elem* _Str, streamsize _Count)形参_Str --写入的内容的起始地址_Count --写入的字节数二进制数据读取文件
read(_Elem* _Str, streamsize _Count) ;形参_Str --读取内容存放缓冲区_Count --要读取的字节数#include iostream
#include fstream
#include cstring
using namespace std;
class Person
{
public:Person() {}Person(const char* name, int age){strcpy_s(this-name, name);this-age age;}char name[20];//姓名int age;//年龄
};
int main()
{/*二进制写入数据示例*/fstream fs(test.doc, ios::out | ios::binary);if (!fs.is_open()){cout 文件创建失败 endl;return 0;}Person p(小王, 18);fs.write((const char *) p, sizeof(p));//写入内容fs.close();//关闭文件/*二进制读取数据示例*/fs.open(test.doc, ios::in | ios::binary);if (!fs.is_open()){cout 文件打开失败 endl;return 0;}Person p2;fs.read((char *) p2, sizeof(p2));cout 读取的内容: endl;cout 姓名: p2.name t年龄: p2.age endl;fs.close();system(pause);}5.C文件指针偏移
//C文件指针偏移seekg(pos_type _Pos,ios_base::seekdir _Way) --用于输入流偏移位置指针到指定位置seekp(pos_type _Pos,ios_base::seekdir _Way) --用于输出流偏移位置指针到指定位置第一个参数偏移量第二个参数基于哪个位置ios::beg --文件头ios::end --文件尾ios::cur --当前位置streamoff tellg() --用于输入流返回当前指针位置streamoff 是一个long long类型streamoff tellp() --用于输出流返回当前指针位置返回值返回基于文件头的偏移量字节为单位。失败则返回-1示例
#include iostream
#include fstream
using namespace std;
int main()
{ifstream fs;fs.open(test.txt, ios::in );//打开文件不存在则打开失败不会截断原内容if (!fs.is_open()){cout 文件打开失败 endl;return 0;}fs.seekg(0,ios::end);//将文件指针偏移到文件末尾char buff[1024];streamoff size fs.tellg();//获取文件大小cout 文件大小: size 字节 endl;fs.seekg(0, ios::beg);//将输入流偏移到文件头while (fs buff){cout buff endl;}fs.close();system(pause);return 0;
}