• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C++文本文件,二进制文件,write(),read(),map容器,seekg(),seekp(),tellg(),tellp()函数

武飞扬头像
Big big world518
帮助3

#include<fstream>

 一:文本文件

1.ofstream和ifstream数据类型 文本文件

  1.  
    ofstream f1;//该数据类型表示输出文件流,用于创建并向文件中写入信息
  2.  
    ifstream f2;//该数据类型表示输入文件流,用于从文件读取信息
  3.  
    //f1,f2表示变量

2.从程序向文件写入字符串

  1.  
    void Write()
  2.  
    {
  3.  
    string file;//文件名
  4.  
    string s;//要写入文件的字符串
  5.  
    f1.open(file.c_str(),ios::out);
  6.  
    //.c_str()是因为文件名file被声明为string,而f1.open()参数要char[]类型,所以要用.c_str()转换一下
  7.  
    //ios::out是定义文件被打开的模式,"out"指写入模式,这种方式会将原有的内容覆盖
  8.  
    f1.open(file.c_str(),ios::app|ios::out);//这种模式会在原来的内容上追加
  9.  
    f1<<s<<endl;
  10.  
    f1.close();
  11.  
    }

3.由程序从文件中读内容

  1.  
    void Read()
  2.  
    {
  3.  
    f2.open(file.c_str(),ios::in);//定义文件模式为读
  4.  
    while(1)
  5.  
    {
  6.  
    getline(f2,s);//一行一行读取文件内容
  7.  
    if(!f2.eof()) //fstream.eof()如果读到文件结束符了返回真,否则返回假
  8.  
    {
  9.  
    cout<<s<<endl;
  10.  
    }
  11.  
    else break;
  12.  
    }
  13.  
    f2>>n;
  14.  
    //假如文件中有一个数字,想要在程序中显示这个数字的平方,一定要先输入流将数字输入到文件中,见上
  15.  
    cout<<n*n<<endl;
  16.  
    f2.close();
  17.  
    }
学新通

 4.实例:英汉词典

  1.  
    class dictionary
  2.  
    {
  3.  
    private:
  4.  
    string file;
  5.  
    ifstream f1;
  6.  
    public:
  7.  
    dictionary(string ff)
  8.  
    {
  9.  
    file=ff;
  10.  
    }
  11.  
    string word(string s)
  12.  
    {
  13.  
    string temp;
  14.  
    string::size_type n1;
  15.  
    n1=s.find(" ",0);
  16.  
    temp=s.substr(0,n1);
  17.  
    return temp;
  18.  
    }
  19.  
    void Find()
  20.  
    {
  21.  
    string temp,s;
  22.  
    cout<<"input";cin>>temp;
  23.  
    f1.open(file.c_str(),ios::in);
  24.  
    if(!f1)//如果f1文件没问题可以读写,f1返回1;
  25.  
    {
  26.  
    cout<<"file error"<<endl;
  27.  
    exit(0);
  28.  
    }
  29.  
    else
  30.  
    {
  31.  
    while(1)
  32.  
    {
  33.  
    getline(f1,s);
  34.  
    if(!f1.eof())
  35.  
    {
  36.  
    if(temp==word(s))
  37.  
    {
  38.  
    cout<<s<<endl;
  39.  
    }
  40.  
    }
  41.  
    else break;
  42.  
    }
  43.  
    }
  44.  
    }
  45.  
    };
  46.  
    int main()
  47.  
    {
  48.  
    dictionary t("英汉词典.txt");
  49.  
    t.Find();
  50.  
    return 0;
  51.  
    }
学新通

在介绍二进制文件之前,简单说一下二进制文件和文本文件的区别: 

二进制文件读写速度非常快,因为数据在内存中是以二进制存储的,二进制文件中存储的数据也是二进制形式的,所以向二进制文件写入数据的时候,内存中的数据直接不需转化放入二进制文件中。而文本文件中的数据是以字符串的形式存储的,数据从内存放入文本文件需要经过一定的编码方式转化(Unicode或ASCII码)成字符串。在读出时,二进制文件的数据不需要解码就可以直接放在内存中,但是这些二进制串我们看不懂,而文本文件需要将字符串解码,再放入内存中,所以效率稍低。我们常用的记事本是文本文件,当从内存中存入非字符串时会乱码,因为字符串在内存中是以编码方式排列好的,而数字则是以二进制形式存储,所以被解码之后会变成我们不知道的码(乱码)

二.二进制文件

fstream f;//二进制文件读写只需要一个变量

1.从程序向二进制文件中写内容

  1.  
    void Write()
  2.  
    {
  3.  
    string::size_type len;
  4.  
    len=s.size();
  5.  
    f.open(file,ios::in|ios::out|ios::binary);//如果是类的话以上操作可以放在字符串中
  6.  
    //ios模式把写和读模式放在一起,还加了一个ios::binary这个是规定Windows的二进制文件换行符为一个\n
  7.  
    f.write((char*)s.c_str(),len);//第一个参数是字符指针类型,第二个是要写的字符串长度
  8.  
    }

 write()函数的返回值通常是len,如果是-1,说明磁盘空间满了或者文件大小超出限制。

第一个参数是写入的来源,第二个参数是要写入的长度

2.从二进制文件向程序读内容并在程序中输出

  1.  
    void Read()
  2.  
    {
  3.  
    string::size_type len;
  4.  
    string s1;
  5.  
    f.seekg(0,ios::end);//将指针移动到文件尾
  6.  
    len=f.tellg();//得到当前指针位置(读取流)
  7.  
    s1.resize(len);//resize第一个参数(此参数)是重新设置的内容长度,第二个参数是初始化值
  8.  
    f.seekg(0,ios::beg);
  9.  
    f.read((char*)s1.c_str(),len);
  10.  
    cout<<s1<<endl;
  11.  
    }

read()函数返回值是读取的字节数,如果是-1说明出现问题; 

3.实例:英汉词典(字符串,容器,文件结合)

  1.  
    class test
  2.  
    {
  3.  
    private:
  4.  
    string s,s1,s2;
  5.  
    string::size_type n,n1,n2,n3,pos;
  6.  
    map<string,string>a;
  7.  
    map<string,string>::iterator p;
  8.  
    fstream f;
  9.  
    public:
  10.  
    test()
  11.  
    {
  12.  
    f.open("英汉词典.txt",ios::in|ios::out|ios::binary);//此txt文件自己预先存到对应编译器路径中
  13.  
    if(!f)
  14.  
    {
  15.  
    cout<<"file error"<<endl;
  16.  
    exit(0);
  17.  
    }
  18.  
    f.seekg(0,ios::end);
  19.  
    n=f.tellg();
  20.  
    s.resize(n);
  21.  
    f.seekg(0,ios::beg);
  22.  
    f.read((char*)s.c_str(),n);
  23.  
    pos=0;
  24.  
    while(1)
  25.  
    {
  26.  
    if(s.find("\n",pos)!=string::npos)
  27.  
    {
  28.  
    n1=s.find("\n",pos);//寻找一行的结尾
  29.  
    }
  30.  
    else
  31.  
    {
  32.  
    n1=s.size();//如果到最后一行了没有换行符,避免查找不到最后一行
  33.  
    }
  34.  
    s1=s.substr(pos,n1-pos);//行串
  35.  
    n2=s1.find(" ",0);
  36.  
    s2=s1.substr(0,n2);
  37.  
    a.insert(pair<string,string>(s2,s1));//键值存入单词,second存入行串(释义);
  38.  
    pos=n1 1;
  39.  
    if(pos>=s.size())//判断是否读完
  40.  
    break;
  41.  
    }
  42.  
    }
  43.  
    void browse()//自己浏览调试
  44.  
    {
  45.  
    for(p=a.begin();p!=a.end(); p)
  46.  
    {
  47.  
    cout<<p->first<<p->second<<endl;
  48.  
    }
  49.  
    }
  50.  
    void Find()
  51.  
    {
  52.  
    string temp;
  53.  
    cout<<"input the word you want to check:"<<endl;cin>>temp;
  54.  
    p=a.find(temp);
  55.  
    if(p!=a.end())
  56.  
    {
  57.  
    cout<<p->second<<endl;
  58.  
    }
  59.  
    }
  60.  
    ~test()//析构函数关闭文件
  61.  
    {
  62.  
    f.close();
  63.  
    }
  64.  
    };
  65.  
    int main()
  66.  
    {
  67.  
    test t;
  68.  
    t.Find();
  69.  
    return 0;
  70.  
    }
学新通

4.文件加解密

  1.  
    class test
  2.  
    {
  3.  
    private:
  4.  
    string::size_type n,n1,n2,n3;
  5.  
    string s,s1,s2;
  6.  
    int i;
  7.  
    fstream f;
  8.  
    public:
  9.  
    test()
  10.  
    {
  11.  
    f.open("test.doc",ios::in|ios::out|ios::binary);
  12.  
    if(!f)
  13.  
    {
  14.  
    cout<<"file error"<<endl;
  15.  
    exit(0);
  16.  
    }
  17.  
    f.seekg(0,ios::end);
  18.  
    n=f.tellg();
  19.  
    s.resize(n);
  20.  
    f.seekg(0,ios::beg);
  21.  
    f.read((char*)s.c_str(),n);
  22.  
    }
  23.  
    void Reverse()//字符串倒序
  24.  
    {
  25.  
    string temp;
  26.  
    temp=s;
  27.  
    for(i=0;i<n; i)
  28.  
    {
  29.  
    temp[i]=s[n-i-1];
  30.  
    }
  31.  
    s=temp;
  32.  
    }
  33.  
    void turn()//字符串前n个字符放到后面
  34.  
    {
  35.  
    int n=100;
  36.  
    s1=s.substr(0,100);
  37.  
    s2=s.substr(100,n-100);
  38.  
    s=s2 s1;
  39.  
    }
  40.  
    void Write()
  41.  
    {
  42.  
    f.seekg(0,ios::beg);
  43.  
    f.write((char*)s.c_str(),n);
  44.  
    }
  45.  
    ~test()
  46.  
    {
  47.  
    f.close();
  48.  
    }
  49.  
    };
  50.  
    int main()
  51.  
    {
  52.  
    test t;
  53.  
    t.Reverse();//t.turn();
  54.  
    t.Write();
  55.  
    return 0;
  56.  
    }
  57.  
     
  58.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhkbeb
系列文章
更多 icon
同类精品
更多 icon
继续加载