zoukankan      html  css  js  c++  java
  • 文件操作

    二进制文件写:

    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        if (argc != 3) {
            cout << "File name missing!" << endl;
            return 0;
        }
        ifstream inFile(argv[1], 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;
    }
    

      

    二进制文件读:

    #include<iostream>
    #include<fstream>
    #include<cstring>
    
    using namespace std;
    
    class CStudent {
    public:
        char szName[20];
        int nScore;
    };
    
    int main()
    {
        CStudent s;
        ifstream InFile("students.dat", ios::out|ios::binary);
        if(!InFile) {
            cout << "error" << endl;
            return 0;
        }
        while (InFile.read((char*)&s, sizeof(s))) {
            int nReadedBytes = InFile.gcount();
            cout << s.szName << " " << s.nScore << endl;
        }
        InFile.close();
        return 0;
    }
    

      

    二进制文件修改:

    #include<iostream>
    #include<fstream>
    #include<cstring>
    
    using namespace std;
    
    class CStudent {
    public:
        char szName[20];
        int nScore;
    };
    
    int main()
    {
        CStudent s;
        fstream iofile("students.dat", ios::in|ios::out|ios::binary);
        if(!iofile) {
            cout << "error" << endl;
            return 0;
        }
        iofile.seekp(2*sizeof(s), ios::beg);
        iofile.write("Mike", strlen("Mike")+1);
        iofile.seekg(0, ios::beg);
        while (iofile.read((char*)&s, sizeof(s))) {
            int nReadedBytes = iofile.gcount();
            cout << s.szName << " " << s.nScore << endl;
        }
        iofile.close();
        return 0;
    }
    

      

    文件拷贝:

    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main(int argc, char const *argv[])
    {
        if (argc != 3) {
            cout << "File name missing!" << endl;
            return 0;
        }
        ifstream inFile(argv[1], 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;
    }
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    测试覆盖(率)到底有什么用?
    重构遗留程序的一次案例学习(java程序)
    rsync学习
    一次awk脚本的重构
    哪本书是对程序员最有影响、每个程序员都该阅读的书?
    我的阅读编程书籍的好方法
    领域驱动设计和实践
    不要if else的编程
    编码规范的要点
    最牛B的编码套路
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10306557.html
Copyright © 2011-2022 走看看