CS 输入-输出流

输入-输出流:

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;

}
  • 关闭文件

    ifs.close();

总结:
\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 . 以二进制形式对文件进行读取操作

代码:
#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;
}

😊求赞!

查看文件:
img

由此可见是二进制形式,那么如何把文件读取回来呢?

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;

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xinwei Xiong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值