从文件中读取下一行

时间:2017-10-06 02:41:00

标签: c++ fstream

因此程序应该从文件中读取,该文件上有4-5行信息。我可以将第一行读入程序,并通过各种算法对其进行处理,但我不确定如何将下一行循环到其中并一次又一次地处理它,直到文件结束。非常感谢您的阅读和所有输入表示赞赏。这是整个程序,文件从底部的文件中读入。

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

int main()
{
    ifstream inputFile;
    ofstream invoicefile;
    string name, author, isbn, customerid, filename, fictionoutput, genreoutput;
    char booktype, genre;
    bool fictionvalue;
    double initial_total, tax_price, subtotal, totalprice, price;
    int fee, quantity;
    const double tax(0.07);

    cout << "Enter name of file.\n";
    cin >> filename;
    cout << "Opening file \n";
    inputFile.open(filename);

    if (inputFile.is_open()) {
        inputFile >> customerid >> name >> author >> isbn >> price >> quantity >> booktype >> genre;

        //QUANTITY FEE CODING BLOCK
        if (quantity > 50) {
            fee = 50;
        }
        else if (quantity >= 15 && quantity <= 19) {
            fee = 40;
        }
        else if (quantity >= 10 && quantity <= 14) {
            fee = 30;
        }
        else if (quantity >= 5 && quantity <= 10) {
            fee = 20;
        }
        else if (quantity < 5) {
            fee = 10;
        }

        //BOOKTYPE CODING BLOCK (FICTION or NON-F)
        if (booktype == 'F') {
            fictionvalue = true;
        }
        else if (booktype == 'N') {
            fictionvalue = false;
        }
        else {
            cout << "INVALID";
        }
        //BOOKTYPE INTO STRING OUTPUT 
        if (fictionvalue = true) {
            fictionoutput = "Fiction";
        }
        else if (fictionvalue = false) {
            fictionoutput = "Non-Fiction";
        }

        //GENRE TYPE INTO STRING OUTPUT
        if (genre == 'R') {
            genreoutput = "Romance";
        }
        else if (genre == 'D') {
            genreoutput = "Drama";
        }
        else if (genre = 'M') {
            genreoutput = 'M';
        }
        else {
            cout << "Invalid entry\n";
        }

        //NO FEE EXCEPTION
        if (booktype == 'N' && genre == 'R') {
            fee = 0;
        }


        //CALCULATION OF PRICE + TAX CODING BLOCK
        initial_total = (price*quantity);
        tax_price = (initial_total * tax);
        subtotal = (initial_total + tax_price);
        totalprice = (subtotal + fee);



        //OUTPUT TO FILE/CONSOLE CODING BLOCK
        cout << "-----------------------------------------" << endl;
        cout << "Order Invoice" << endl;
        cout << "Customer ID: " << customerid << endl;
        cout << name << " " << author << " " << fictionoutput << " " << genreoutput << " " << quantity << "@" << price << "Subtotal: " << endl; //add subtotal price
        //cout << "Total book sales: " <<
        cout << "Tax: " << tax_price << endl;
        cout << "Subtotal: " << subtotal << endl;
        cout << "Fee: " << fee << endl;
        cout << "Total Price: " << totalprice << endl;

        cout << "-----------------------------------------" << endl;
        system("pause");
    }

}

TEXT SAMPLE

1234 Dog_Strategy Henry_Moreno 3-598-21500-2 12.99 5 N M
6789 Companion_Kicked_Me_Out Lorraine_Johnson 3-598-21599-1 24.99 3 F R
3444 Mime_On_My Journey Kristy_Wahl 3-699-21500-8 6.75 10 N D
4455 Damaged_By_The_Joke Henry_Christopher   3-598-21500-2 12.99 4 N R

2 个答案:

答案 0 :(得分:1)

也许尝试使用这样的循环:

// Create an empty string
std::string line;

// Start a loop that will get a line from the file and input it in our string
// this loop will keep going until the getline fails, i.e. end of file.
while (std::getline(fileName, line)) 
{
CODE
}

答案 1 :(得分:-2)

你可以放一个while循环,直到程序看到文件结束

while(!EOF)
{your code here}

并且永远不要忘记关闭您打开的文件

相关问题