我如何从外部文件中读取

时间:2013-12-19 20:37:20

标签: c++

基本上,我需要一个基本的.txt - 也许是一个.csv文件 - 将由程序读取并使用文件中的数字。 我希望文件看起来像这样:

橘子:1.50

苹果:2.10

梨:4.70

然后我希望程序从第一行读取1.50并将其分配给变量orangeCost。然后我想要第二部分读取第二行并将2.10分配给变量apples等。

这是我到目前为止所做的:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    int garden();

    //Lawn
    int lawnLength;         
    int lawnWidth;
    int lawnTime = 20;
    float lawnCost = 15.50;

    cout << "Length of lawn required: "; // Asks for the length
    cin >> lawnLength; // Writes to variable
    cout << "Width of lawn required: "; // Asks for the width
    cin >> lawnWidth; // Writes to variable
    int lawnArea = (lawnLength * lawnWidth); //Calculates the total area
    cout << endl << "Area of lawn required is " << lawnArea << " square meters"; //Prints the total area
    cout << endl << "This will cost a total of " << (lawnArea * lawnCost) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (lawnArea * lawnTime) << " minutes" << endl << endl; //Prints total time

    //Concrete Patio
    int concreteLength;         
    int concreteWidth;
    int concreteTime = 20;
    float concreteCost = 20.99;

    cout << "Length of concrete required: "; // Asks for the length
    cin >> concreteLength; // Writes to variable
    cout << "Width of concrete required: "; // Asks for the width
    cin >> concreteWidth; // Writes to variable
    int concreteArea = (concreteLength * concreteWidth); //Calculates the total area
    cout << endl << "Area of concrete required is " << concreteArea << " square meters"; //Prints the total area
    cout << endl << "This will cost a total of " << (concreteArea * concreteCost) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (concreteArea * concreteTime) << " minutes" << endl << endl; //Prints total time

    //Wooden Deck
    int woodenDeckLength;           
    int woodenDeckWidth;
    int woodenDeckTime = 30;
    float woodenDeckCost = 15.75;

    cout << "Length of wooden deck required: "; // Asks for the length
    cin >> woodenDeckLength; // Writes to variable
    cout << "Width of wooden deck required: "; // Asks for the width
    cin >> woodenDeckWidth; // Writes to variable
    int woodenDeckArea = (woodenDeckLength * woodenDeckWidth); //Calculates the total area
    cout << endl << "Area of wooden deck required is " << woodenDeckArea << " square meters"; //Prints the total area
    cout << endl << "This will cost a total of " << (woodenDeckArea * woodenDeckCost) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl; //Prints total time

    //Rectangular Pond
    int rectangularPondLength;          
    int rectangularPondWidth;
    int rectangularPondTime = 45;   
    float rectangularPondCost = 25.00;

    cout << "Length of rectangular pond required: "; // Asks for the length
    cin >> rectangularPondLength; // Writes to variable
    cout << "Width of rectangular pond required: "; // Asks for the width
    cin >> rectangularPondWidth; // Writes to variable
    int rectangularPondArea = (rectangularPondLength * rectangularPondWidth); //Calculates the total area
    cout << endl << "Area of rectangular pond required is " << rectangularPondArea << " square meters"; //Prints the total area
    cout << endl << "This will cost a total of " << (rectangularPondArea * rectangularPondCost) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl; //Prints total time

    //Water Features
    int waterFeatures;          
    int waterFeaturesTime = 60; 
    float waterFeaturesCost = 150.00;

    cout << "Number of water features required: "; // Asks for the amount of water features needed
    cin >> waterFeatures; // Writes to variable
    cout << endl << "Number of water feature(s) required is " << waterFeatures << " water feature(s)"; //Prints the total area
    cout << endl << "This will cost a total of " << (waterFeatures * waterFeaturesCost) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl; //Prints total time

    //Garden Lights
    int gardenLights;           
    int gardenLightsTime = 10;  
    float gardenLightsCosts = 5.00;

    cout << "Number of garden lights required: "; // Asks for the amount of water features needed
    cin >> gardenLights; // Writes to variable
    cout << endl << "Number of garden light(s) required is " << gardenLights << " garden light(s)"; //Prints the total area
    cout << endl << "This will cost a total of " << (gardenLights * gardenLightsCosts) << " pounds"; //Prints the total cost
    cout << endl << "This will take a total of " << (gardenLights* gardenLightsTime) << " minutes" << endl << endl; //Prints total time

    //Quotation
    string quotation;

    cout << "Do you want to save a quotation? " << endl; //Asks if they want to save a quotation
    cin >> quotation; //Saves if they want to save a quotation or not

    if (quotation == "yes")
    {
        std::string nameOfFile;
        cout << "What would you like to save the quotation as? (Maybe using your last name) " << endl;
        cin >> nameOfFile;

        ofstream outfile(nameOfFile + ".txt"); // Opens file in write mode

        //Lawn
        outfile << "The total lawn area is " << lawnArea << " square meters" << endl; // Writes users data into the file 
        outfile << "The total cost for the lawn is £" << (lawnArea * lawnCost) << endl; // Writes users data into the file 
        outfile << "The total time for the lawn is " << (lawnArea * lawnTime) << " minutes" << endl << endl; // Writes users data into the file 
        //Concrete Patio
        outfile << "The total concrete patio area is " << concreteArea << " square meters" << endl; 
        outfile << "The total cost for the concrete patio is £" << (concreteArea * concreteCost) << endl;
        outfile << "The total time for the concrete patio is " << (concreteArea * concreteTime) << " minutes" << endl << endl;
        //Wooden Deck
        outfile << "The total wooden deck area is " << woodenDeckArea << " square meters" << endl;
        outfile << "The total cost for the wooden deck is £" << (woodenDeckArea * woodenDeckCost) << endl;
        outfile << "The total time for the wooden deck is " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl;
        //Rectangular Pond
        outfile << "The total rectangular pond area is " << rectangularPondArea << " square meters" << endl;
        outfile << "The total cost for the wooden deck is £" << (rectangularPondArea * rectangularPondCost) << endl;
        outfile << "The total time for the wooden deck is " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl;
        //Water Features
        outfile << "The total number of water feature(s) needed are " << waterFeatures << endl;
        outfile << "The total cost for the water feature(s) is £" << (waterFeatures * waterFeaturesCost) << endl;
        outfile << "The total time for the water feature(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;
        //Garden Lights
        outfile << "The total number of garden light(s) needed are " << gardenLights << endl;
        outfile << "The total cost for the garden light(s) is £" << (gardenLights * gardenLightsCosts) << endl;
        outfile << "The total time for the garden light(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;       
        //CLOSE FILE
        outfile.close(); // Close's the file.
        cout << "Quotation saved." << endl;
    }
    else
    {
        cout << "Quotation not saved." << endl;
    }


    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    return 0;
}

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

这是一种方法:

使用私有变量fruitname创建一个班级cost

然后创建一个构造函数,它接受一个字符串(输入文件中的一行)。将所有内容提取到name中的所有内容以及cost后面的值。

您读取文件的功能需要执行以下操作:

  • 声明一个水果数组
  • 循环:
    • 从文件中获取一行到变量
    • 使用此变量调用fruit类,并将创建的对象添加到数组

当你到达文件末尾时,你会得到一系列水果,其中包含文件中的所有数据。

要了解课程,请查看:http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm

要了解如何阅读文件,请查看:http://www.cplusplus.com/doc/tutorial/files/