[c++基础]文件流

All Of It

c++将每个文件看成是字节序列,每个文件都以一个文件结束符(end-of-file maker)作为结尾.当打开一个文件时,一个对象便被创建,并且将一个流关联到这个对象上.

例子-顺序-向文件输出数据



#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

//向文件输出数据
int main()
{
    //输入输出是相对于内存来言,每次写代码时,将内存当作自己,输入,就是文件输入给自己,输出,就是我输出给文件
    //ios::app //append
    //ios::ate //可以在文件的任何位置写数据
    //ios::in   //打开一个文件作为输入文件
    //ios::out  //打开一个文件作为输出文件
    //ios::trune    //丢弃文件的内容(这是ios::out默认的设置)
    //ios::binary   //打开一个文件进行二进制输入或输出
    ofstream outClientFile("clients.txt", ios::out);
    //相当于
    //ofstream outClientFile;
    //outClientFile.open("clients.txt",ios::out);

    if (!outClientFile)
    {
        cerr << "File counld not be opened" << endl;
        exit(EXIT_FAILURE);
    }

    cout << "Enter the account,name and balance." << endl
        << "Enter the end of file to end input.\n?";//windows : ctrl+z 有时要加回车 //linux ctrl d

    int account;
    string name;
    double balance;
    
    //
    while (cin>>account>>name>>balance)
    {
        outClientFile << account << " " << name << " " << balance << endl;
        cout << "?";
    }

    //outClientFile.close();//显示调用
    return 0;//隐式调用outClientFile.close();
}

[c++基础]文件流

[c++基础]文件流

例子-顺序-从文件输入数据


#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
//输入文件

//输出每一条记录
void outputLine(int account, const string& name, double balance)
{
    //最后一个右对齐
    cout << left << setw(10) << account << setw(13) << name << setw(7) << setprecision(2) << right << balance << endl;
}

int main()
{
    ifstream inClientFile("clients.txt", ios::in);

    if (!inClientFile)
    {
        cerr << "File counld not opened" << endl;
        exit(EXIT_FAILURE);
    }

    int account;
    string name;
    double balance;

    cout << left << setw(10) << "Account" << setw(13)
        << "Name" << "Balance" << endl << fixed << showpoint;//固定小数位

    while (inClientFile>>account>>name>>balance)
    {
        outputLine(account,name,balance);
    }


    return 0;
}

[c++基础]文件流

例子-随机-贷款查询

此程序有bug,未解决


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;
enum RequestType{ZERO_BALANCE = 1,CREDIT_BALANCE,DEBIT_BALANCE,END};
int getRequest()
{
    int request;

    cout << "\nEnter request" << endl
        << "1- list accounts with zero balances" << endl
        << "2- list accounts with creadit balances" << endl
        << "3-list accounts with debit balance " << endl
        << "4-end of run" << fixed << showpoint;

    do//这里有bug 当我第一次输入?时,程序死循环
    {
        cout << "\n?";
        cin >> request;
    } while (request < ZERO_BALANCE && request >END);//这里是&& 不是|| 
    return request;
}
bool shouldDisplay(int type, double balance)
{
    if (type == ZERO_BALANCE && balance == 0)
    {
        return true;
    }

    if (type == CREDIT_BALANCE && balance <0)
    {
        return true;
    }

    if (type == DEBIT_BALANCE && balance >0)
    {
        return true;
    }
    return false;
}
static void outputLine(int account, const string& name, double balance)
{
    //最后一个右对齐
    cout << left << setw(10) << account << setw(13) << name << setw(7) << setprecision(2) << right << balance << endl;
}
int main()
{
    ifstream inClientFile("clients.txt", ios::in);
    if (!inClientFile)
    {
        cerr << "File could not opened" << endl;
        exit(EXIT_FAILURE);
    }

    int account;
    string name;
    double balance;
    int request = getRequest();

    while (request !=END)
    {
        switch (request)
        {
        case ZERO_BALANCE:
            cout << "\nAccounts with zero balance:\n";
            break;
        case CREDIT_BALANCE:
            cout << "\nAccounts with credit balance\n";
            break;
        case DEBIT_BALANCE:
            cout << "\nAccounts with debit balance\n";
            break;
        default:
            break;
        }
        //每次遍历整个文件 
        inClientFile >> account >> name >> balance;
        while (!inClientFile.eof())
        {
            if (shouldDisplay(request,balance))
            {
                outputLine(account,name,balance);
            }
            inClientFile >> account >> name >> balance;
        }
        //使得文件指针回到文件开始
        inClientFile.clear();//reset eof for next input
        inClientFile.seekg(0);//reposition to beginning of file
        request = getRequest();
    }
    cout << "End of run..." << endl;
    return 0;
}

[c++基础]文件流

bug

[c++基础]文件流

  • 出现死循环

[c++基础]文件流

上一篇:在 Mysql 事务中执行 SELECT 时是否需要加 FOR UPDATE?


下一篇:mysql事务