输入-输出流:
文章目录
ios::in | 读文件 |
---|---|
ios::out | 为写文件而打开文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 追加方式写文件 |
ios::trunc | 如果文件存在先删除,再创建 |
ios::binary | 二进制方式 |
❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️
1. 写文件
o 首先要使用头文件
** **#include<fstream>**
-
创建流对象
ofstream ofs;
-
指定打开方式 **–可以写绝对路径或者相对路径
ofs.open("test.txt",ios::out);
-
写内容
ofs<<"姓名:"<<endl;
-
关闭文件
ofs.close();
2. 读文件
o 创建流对象**
ifstream ifs;**
- 打开文件,判断文件是否打开成功
ifs.open("文件路径",打开方式);
if(!ifs.is_open())
{
cout<<"文件打开失败"<<endl;
return;
}
第一种:
char buf[1024] = { 0 };
//定义字符数组
while(ifs >> buf)
{
cout << buf <<endl;
}
第二种:
char buf[1024] = { 0 };
//getline获取,sizeof统计字符长度
while(ifs.getline(buf,sizeof(buf)))
{
cout << buf <<endl;
}
第三种
//使用string --需要包含string头文件
while ( getline( ifs,buf ))
{
cout<< buf <<endl;
}
第四种:不推荐 --依次读取字符
char c;
while (( c=ifs.get())!=EOF)
{ //eof是文件尾部的标志
cout<<c;
}
总结:
\1. 读文件可以使用ifstream,或者fstream类
\2. 利用is_open函数判断文件是否打开成功
\3. close关闭文件
代码:
#include<iostream>
#include<fstream> //包含头文件
using namespace std;
//文本文件 读文件
void test01()
{
//创建流对象
ifstream ifs;
//打开文件
ifs.open("/c/cpp/tar.cpp",ios::in);
if(!ifs.is_open())
{
cout<<"文件打开失败"<<endl;
return ;
}
char buf[1024] = { 0 };
//定义字符数组
while(ifs >> buf)
{
cout << buf <<endl;
}
ifs.close(); //关闭文件
}
int main()
{
test01();
return 0;
}
3 . 以二进制形式对文件进行读取操作
- 打开的文件会出现乱码
- 也可以通过加密方式保护文件
- 打开方式要指定为 ios::binary
代码:
#include<fstream>
#include<iostream>
using namespace std;
//二进制写文件
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test01()
{
//创建流对象
ofstream ofs;
//打开文件 //用二进制打开方式要指定为 ios::binary
ofs.open("/c/cpp/tar.cpp",ios::out | ios::binary);
//写文件
Person p ={"张三",18};
ofs.write((const char *)&p,sizeop(Person));
//由于&p取地址是person类型,所以要强制转换为char
//关闭文件
ofs.close();
}
int main()
{
test01();
return 0;
}
😊求赞!
查看文件:
由此可见是二进制形式,那么如何把文件读取回来呢?
4 . 文件读取:
读文件用read
\1. 函数原型: **istream& read(char *buffer,int len);**
\2. 参数解释:字符指针buffer指向内存中一段存储空间。len是读取的字节数
代码:
include<fstream>
include<iostream>
using namespace std;
//读取二进制文件
class Person
{
public:
char m_Name[64]; //属性姓名
int m_Age; //属性年龄
};
void test01()
{
//创建流对象
ifstream ifs; //注意读文件和写文件流对象区别
//打开文件
ifs.open("/c/cpp/tar.cpp",ios::in | ios::binary);
//判断文件是否读入成功 --使用前面讲的方法
if (!ifs.is_open())
{
cout << "文件打开失败"<<endl;
return ; //不返回
}
//
//读文件 --四种方法选择一种
Person p;
ifs.read((char *)&p,sizeof(Person));
cout <<"姓名:"<<p.m_Name<<"年龄:"<<p.m_Age<<endl;
//由于&p取地址是person类型,所以要强制转换为char
//关闭文件
ifs.close();
}
int main()
{
test01();
return 0;
}
5 . 加密解密(c语言实现)
代码:
void myEncodeAnDecode(char prePath[], char resultPath[], int password) {
FILE * normal_fp = fopen(prePath, "rb");
FILE * encode_fp = fopen(resultPath, "wb");
int ch;
while ((ch = fgetc(normal_fp)) != EOF){
fputc(ch ^ password, encode_fp);
}
fclose(normal_fp);
fclose(encode_fp);
}
int main() {
char * oriPath = "D:\\picLibrary\\xiaoHuangTu.png";
//原文件,加密后可删除掉,防止别人查看
char * showOthersPath = "D:\\picLibrary\\xiaoHuangEncode.png";
//存放加密的文件,给别人看,别人也看不了的文件
char * newPath = "D:\\picLibrary\\xiaoHuangDecode.png";
//将加密的文件解密出来的文件,可以自己偷偷的查看
//加密
myEncodeAnDecode(oriPath, showOthersPath, 100);//密码随便搞个100
//解密
myEncodeAnDecode(showOthersPath, newPath, 100);
system("pause");
return 0;
}