我试图读取像这样的列表
James John 15 5 1
Douglas Frank 23 8 1
Bnejamin Zach 17 1 4
并将每个值存储到一个单独的变量中。名称是字符串,其他数字是浮点数和int。到目前为止,我可以从一行获取数据,但我不知道如何进入下一行并执行相同的操作。到目前为止,这是我的代码。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream employees;
string lastname, firstname, lastname1, firstname1, lastname2, firstname2;
float base, sales, base1, sales1, base2, sales2;
int years, years1, years2;
employees.open("employees.txt");
while (employees)
{
employees >> lastname >> firstname >> base >> sales >> years;
我想尽量保持简单,我还不知道用户定义的函数,数组或向量。那么有一个简单的功能,只会在几年后结束这条线;然后去下一行继续?
感谢。
答案 0 :(得分:2)
使用数组。每当你最终得到“我想为这个变量添加一个数字,因为我想要多个”时,如果数量超过2,那么你应该使用一个数组(除非非常特殊情况)。
您可能还希望使用结构来存储不同的值(名字,姓氏,基数,销售额和年份) - 这样,您只能获得单个数组,而不是几个不同的数组。
由于这是C ++,因此数组意味着vector
。换句话说:
struct employee
{
string firstname, lastname;
float base, sales;
int years;
};
vector<employee> emp_table;
employee e;
while (employees >> e.firstname >> e.lastname >> e.base >> e.sales >> e.years)
{
emp_table.push_back(e);
}
注意我将员工的输入作为时间条件。这样可以避免额外的循环迭代,并在到达文件末尾时“推回”最后一个条目的第二个副本。
答案 1 :(得分:1)
C ++有很多方法可以完成你想要做的事情。允许数据验证的一种方法是使用std::getline
函数一次读取一行文件,然后使用std::stringstream
解析数据。这允许您验证数据并继续如果一行上的数据格式错误,则进行处理。
[正如Mats所说,你可以使用数据结构和std::vector
来更轻松地存储和管理数据。]
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
struct employee
{
std::string firstname;
std::string lastname;
float base;
float sales;
int years;
};
int main()
{
std::ifstream employeeFile;
employeeFile.open("employees.txt");
std::string tmpLine;
std::vector<employee> employeeTable;
// read in an entire line at a time
while(std::getline(employeeFile, tmpLine))
{
// Place the input line into a stream that reads from
// a string instead of a file.
std::stringstream inputLine(tmpLine);
// Try parsing the data. The ! operator is used here to check
// for errors. Since we expect the data to be in a specific format
// we want to be able to handle situations where the input line
// may be malformed. For example, encountering a string where
// a number should be.
employee e;
if(!(inputLine >> e.firstname >> e.lastname >> e.base >> e.sales >> e.years))
{
// ... error parsing input. Report the error
// or handle it in some other way.
continue; // keep going!
}
// Add to the vector
employeeTable.push_back(e);
}
return 0;
}
答案 2 :(得分:0)
您可以在循环中使用getline检索每一行,然后将其与stringstream
类似的东西:
string line;
while(getline(employees,line))
{
//doSomething
}
如果您不能使用数组轻松存储它们,您可以使用计数器来了解您所在的行,但这非常重复,并且文件中的行数不能有所不同:
string line;
for (int count = 1 ; count <= 3 ; count++)
{
getline(employees,line);
istringstream iss(line);
if (count == 1)
{
iss >> lastname >> firstname >> base >> sales >> years;
}
else if (count == 2)
{
iss >> lastname1 >> firstname1 >> base1 >> sales1 >> years1;
}
else if (count == 3)
{
iss >> lastname2 >> firstname2 >> base2 >> sales2 >> years2;
}
}
答案 3 :(得分:0)
正确打开和读取文件比学习数组更难。如果不使用数组,则必须使用太多变量来保存所有数据,并且必须重复编写代码以从文件中读取,而不是在循环中编写一次。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string firstNames[3];
string lastNames[3];
float heights[3];
float weights[3];
int ages[3];
ifstream infile("data.txt");
if(!infile)
{
cout << "Couldn't open file!" << endl;
return 1;
}
int count = 0;
while (infile >> firstNames[count]
>> lastNames[count]
>> heights[count]
>> weights[count]
>> ages[count] )
{
++count;
}
infile.close();
for (int i = 0; i<count; ++i) {
cout << firstNames[i] << " "
<< lastNames[i] << " "
<< heights[i] << " "
<< weights[i] << " "
<< ages[i] << " " << endl;
}
return 0;
}
--output:--
James John 15 5 1
Douglas Frank 23 8 1
Bnejamin Zach 17 1 4
与此灾难相比:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string firstName0,firstName1, firstName2;
string lastName0, lastName1, lastName2;
float height0, height1, height2;
float weight0, weight1, weight2;
int age0, age1, age2;
ifstream infile("data.txt");
if(!infile)
{
cout << "Couldn't open file!" << endl;
return 1;
}
infile >> firstName0 >> lastName0 >> height0 >> weight0 >> age0;
infile >> firstName1 >> lastName1 >> height1 >> weight1 >> age1;
infile >> firstName2 >> lastName2 >> height2 >> weight2 >> age2;
infile.close();
cout << firstName0 << " "
<< lastName0 << " "
<< height0 << " "
<< weight0 << " "
<< age0 << endl;
cout << firstName1 << " "
<< lastName1 << " "
<< height1 << " "
<< weight1 << " "
<< age1 << endl;
cout << firstName2 << " "
<< lastName2 << " "
<< height2 << " "
<< weight2 << " "
<< age2 << endl;
return 0;
}
--output:--
James John 15 5 1
Douglas Frank 23 8 1
Bnejamin Zach 17 1 4
查看您必须重复的所有代码。
请注意,当您使用数组时,变量名称将变为firstNames [0](v。firstName0),lastNames [0](v.lastName0)等,以及firstNames [1](v。firstName1)和lastNames [1](v.lastName0)。