zoukankan      html  css  js  c++  java
  • [C++]文件处理实例-基础版

    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    inline void eatline() { while (cin.get() != '
    ') continue; }
    struct planet
    {
        char name[20];
        double population;
        double g;
    };
    const char* file = "planets.dat";
    int main(void) {
        planet pl;
        cout << fixed;
        
        ifstream fin;
        fin.open(file,ios_base::in|ios_base::binary);
        if (fin.is_open()) {
            cout << "Here are the current contents of the " << file << " file:
    ";
            while (fin.read((char*) &pl,sizeof pl))
            {
                cout << setw(20) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
            }
            fin.close();
        }
        ofstream fout(file, ios::out | ios::app | ios::binary);
        if (!fout.is_open()) {
            cerr << "Can't open " << file << " file for output.
    ";
            exit(EXIT_FAILURE);
        }
        cout << "Enter guest names (enter a blank line to quit):
    ";
        cin.get(pl.name, 20);
        while (pl.name[0]!='')
        {
            eatline();
            cout << "Enter planetary population: ";
            cin >> pl.population;
            cout << "Enter planet's acceleration of gravity: ";
            cin >> pl.g;
            eatline();
            fout.write((char*)&pl, sizeof pl);
            cout << "Enter planet name (enter a blank line to quit):
    ";
            cin.get(pl.name, 20);
        }
        fout.close();
        fin.clear();
        fin.open(file);
        if (fin.is_open()) {
            cout << "Here are the new contents of the " << file << " file:
    ";
            while (fin.read((char*) &pl,sizeof pl))
            {
                cout << setw(20) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
            }
            fin.close();
        }
        cout << "Done.
    ";
        return 0;
    }
    const int LIM = 20;
    struct planet
    {
        char name[LIM];
        double population;
        double g;
    };
    const char* file = "planets.dat";
    inline void eatline() { while (cin.get() != '
    ') continue; }
    int main(void) {
        planet pl;
        cout << fixed << right;
        fstream finout;
        finout.open(file, ios_base::in | ios_base::out | ios_base::binary);
        int ct = 0;
        if (finout.is_open()) {
            finout.seekg(0);
            cout << "Here are the current contents of the " << file << " file:
    ";
            while (finout.read((char*)&pl, sizeof pl))
            {
                cout << ct++ << ": " << setw(20) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
                //cout << setw(20) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
            }
            if (finout.eof())
                finout.clear();
            else {
                cerr << "Error in reading " << file << ".
    ";
                exit(EXIT_FAILURE);
            }
        }
        else {
            cerr << file << " could not be opened -- bye.
    ";
            exit(EXIT_FAILURE);
        }
        cout << "Enter the record number you wish to change: ";
        long rec;
        cin >> rec;
        eatline();
        if (rec < 0 || rec >= ct) {
            cerr << "Invalid record number -- bye
    ";
            exit(EXIT_FAILURE);
        }
        streampos place = rec * sizeof pl;
        finout.seekg(place);
        if (finout.fail()) {
            cerr << "Error on attempted seek
    ";
            exit(EXIT_FAILURE);
        }
        finout.read((char*)&pl, sizeof pl);
        cout << "Your selection:
    ";
        cout << rec << ": " << setw(LIM) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
        if (finout.eof())
            finout.clear();
        cout << "Enter planet name: ";
        cin.get(pl.name, LIM);
        eatline();
        cout << "Enter planetary population: ";
        cin >> pl.population;
        cout << "Enter planet's acceleration of gravity: ";
        cin >> pl.g;
        finout.seekp(place);
        finout.write((char*)&pl, sizeof pl) << flush;
        if (finout.fail()) {
            cerr << "Error on attempted write
    ";
            exit(EXIT_FAILURE);
        }
        ct = 0;
        finout.seekg(0);
        cout << "Here are the new contents of the " << file << " file:
    ";
        while (finout.read((char*)&pl, sizeof pl))
        {
            cout << ct++ << ": " << setw(LIM) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl;
        }
        finout.close();
        cout << "Done.
    ";
        return 0;
    }
  • 相关阅读:
    将make的输出重定向到文件
    ubuntu mount u盘以及cp拷贝文件夹
    Emacs Tutorial摘录
    c#实现每隔一段时间执行代码(多线程)
    socket.BeginReceiveFrom异步接收信息时,回调函数无法正常进入
    23个C#实用技巧
    C#中实现Form的透明属性变化即渐隐效果
    C#键位定制:设置一个文本框只能输入数字键
    byte 与 bit 的转换
    C# Socket UDP 案例 2
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10491695.html
Copyright © 2011-2022 走看看