zoukankan      html  css  js  c++  java
  • c++之io操作

    c++ fstream中seekg()和seekp()的用法 
    
    先说一下C语言中fseek()的功能:
    函数原型:int fseek(FILE *fp, LONG offset, int origin)
    参数含义:fp 文件指针 offset 相对于origin规定的偏移位置量 origin 指针移动的起始位置,可设置为以下三种情况: SEEK_SET 文件开始位置 SEEK_CUR 文件当前位置 SEEK_END 文件结束位置
    
    C++中seep()和seekg()函数功能
    seekp:设置输出文件流的文件流指针位置
    seekg:设置输入文件流的文件流指针位置
    函数原型:
    ostream& seekp( streampos pos );
    ostream& seekp( streamoff off, ios::seek_dir dir );
    istream& seekg( streampos pos );
    istream& seekg( streamoff off, ios::seek_dir dir );
    
    函数参数
    pos:新的文件流指针位置值
    off:需要偏移的值
    dir:搜索的起始位置
    dir参数用于对文件流指针的定位操作上,代表搜索的起始位置
    在ios中定义的枚举类型:
    enum seek_dir {beg, cur, end};
    每个枚举常量的含义:
    ios::beg:文件流的起始位置
    ios::cur:文件流的当前位置
    ios::end:文件流的结束位置
    
    
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    int main(){
        ifstream inFile("binary.dat", ios::in | ios::binary);
        if(!inFile){
            cout<< "error" <<endl;
            return 0;
        }
        float fea[128];
        int n=0;
        while(inFile.read((char *)&fea[0], 128*sizeof(float))){
            //print dim1 of each img
            n++;
        }
        inFile.close();
        
        cout<<n<<endl;
        for(int i=0;i<128;i++)
            cout<<fea[i]<<" ";
        return 0;
    }
    
    
    
    用 ostream::write 成员函数写文件  ostream & write(char* buffer, int count);   //
    用二进制文件保存学生记录:
    #include <iostream>
    #include <fstream>
    using namespace std;
    class CStudent
    {
    public:
        char szName[20];
        int age;
    };
    int main()
    {
        CStudent s;
        ofstream outFile("students.dat", ios::out | ios::binary);
        while (cin >> s.szName >> s.age)
            outFile.write((char*)&s, sizeof(s));
        outFile.close();
        return 0;
    }
    
    用 istream::read 成员函数读文件     istream & read(char* buffer, int count);
    #include <iostream>
    #include <fstream>
    using namespace std;
    class CStudent
    {
        public:
            char szName[20];
            int age;
    };
    int main()
    {
        CStudent s;       
        ifstream inFile("students.dat",ios::in|ios::binary); //二进制读方式打开
        if(!inFile) {
            cout << "error" <<endl;
            return 0;
        }
        while(inFile.read((char *)&s, sizeof(s))) { //一直读到文件结束
            int readedBytes = inFile.gcount(); //看刚才读了多少字节
            cout << s.szName << " " << s.age << endl;   
        }
        inFile.close();
        return 0;
    }
    
    用文件流类的 put 和 get 成员函数读写文件
    可以用 ifstream 和 fstream 类的 get 成员函数(继承自 istream 类)从文件中一次读取一个字节,
    也可以用 ofstream 和 fstream 类的 put 成员函数(继承自 ostream 类) 向文件中一次写入一个字节。
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main(int argc, char* argv[])
    {
        if (argc != 3) {
            cout << "File name missing!" << endl;
            return 0;
        }
        ifstream inFile(argv[l], ios::binary | ios::in);  //以二进制读模式打开文件
        if (!inFile) {
            cout << "Source file open error." << endl;
            return 0;
        }
        ofstream outFile(argv[2], ios::binary | ios::out);  //以二进制写模式打开文件
        if (!outFile) {
            cout << "New file open error." << endl;
            inFile.close();  //打开的文件一定要关闭
            return 0;
        }
        char c;
        while (inFile.get(c))  //每次读取一个字符
            outFile.put(c);  //每次写入一个字符
        outFile.close();
        inFile.close();
        return 0;
    }
    
    
    C++移动和获取文件读写指针(seekp、seekg、tellg、tellp)
    在读写文件时,有时希望直接跳到文件中的某处开始读写,这就需要先将文件的读写指针指向该处,然后再进行读写。 
    ifstream 类和 fstream 类有 seekg 成员函数,可以设置文件读指针的位置;
    ofstream 类和 fstream 类有 seekp 成员函数,可以设置文件写指针的位置。
    这两个函数的原型如下: 
    ostream & seekp (int offset, int mode);
    istream & seekg (int offset, int mode);
    mode 代表文件读写指针的设置模式,有以下三种选项: 
    ios::beg:让文件读指针(或写指针)指向从文件开始向后的 offset 字节处。offset 等于 0 即代表文件开头。在此情况下,offset 只能是非负数。
    ios::cur:在此情况下,offset 为负数则表示将读指针(或写指针)从当前位置朝文件开头方向移动 offset 字节,为正数则表示将读指针(或写指针)从当前位置朝文件尾部移动 offset字节,为 0 则不移动。
    ios::end:让文件读指针(或写指针)指向从文件结尾往前的 |offset|(offset 的绝对值)字节处。在此情况下,offset 只能是 0 或者负数。
    
    此外,我们还可以得到当前读写指针的具体位置: 
    ifstream 类和 fstream 类还有 tellg 成员函数,能够返回文件读指针的位置;
    ofstream 类和 fstream 类还有 tellp 成员函数,能够返回文件写指针的位置。
    这两个成员函数的原型如下: 
    int tellg();
    int tellp();
    要获取文件长度,可以用 seekg 函数将文件读指针定位到文件尾部,再用 tellg 函数获取文件读指针的位置,此位置即为文件长度。
  • 相关阅读:
    杂谈
    MD语法
    1034 有理数四则运算(20 分)
    1033 旧键盘打字(20 分)
    1032 挖掘机技术哪家强(20 分)
    1031 查验身份证(15 分)
    1030 完美数列(25 分)
    1029 旧键盘(20 分)
    1028 人口普查(20 分)
    1027 打印沙漏(20 分)
  • 原文地址:https://www.cnblogs.com/lovebay/p/12744489.html
Copyright © 2011-2022 走看看