ITPub博客

首页 > > > 为什么文件里会出现一些奇怪的字符?代码如下

为什么文件里会出现一些奇怪的字符?代码如下

作者:doudourabbit 时间:2019-09-03 11:36:54 0 删除 编辑
#include<iostream>//设计一个测试seekg()的程序
#include<fstream>
#include<string>
class Chih
{
    std::ifstream in;
    std::ofstream out;
    int a;
    std::string file;
    std::string file1;
public:
    Chih();
    ~Chih();
    void Check();
};
Chih::Chih()
{
    std::cout << "Write your file name: ";
    std::cin >> file;
    in.open(file, std::ios::binary);
    if (!in)
    {
        std::cout << "failed to open your file -o-!" << std::endl;
        exit(1);
    }
    std::cout << "Input your storage file name: ";
    std::cin >> file1;
    out.open(file1, std::ios::binary);
    if (!out)
    {
        std::cout << "failed to open storage file 0-0!";
        exit(1);
    }
}
Chih::~Chih()
{
    in.close();
    out.close();
}
void Chih::Check()
{
    char buf[1000];
    in.seekg(2, std::ios::beg);//seekg难道也是ifstream的成员函数吗?
    in.read((char*)&buf, sizeof(buf)/5);
    //out.seekg(20,std::ios::cur);//seekg()是类istream和ostream的成员函数
    out.write((char*)&buf, sizeof(buf)/5);
}
int main()
{
    Chih du;
    du.Check();
    return 0;
}

第二个注释是编这个程序的时候遇到的另一个问题。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4809/viewspace-3167986/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    193
  • 访问量
    1438