如何从外部文件中读取

How Would I Read From An External File

本文关键字:读取 文件 从外部      更新时间:2023-10-16

基本上,我需要一个基本的.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;
}

感谢您的帮助!:)

这里有一种方法:

创建一个具有私有变量namecost的类fruit

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

你读取文件的功能需要这样做:

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

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

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

要了解有关从文件读取的信息,请检查:http://www.cplusplus.com/doc/tutorial/files/