[英]How to read int and strings from a text file?
I have a text file containing name, ability, attack, and defense on every line, for instance:我有一个文本文件,每行都包含名称、能力、攻击和防御,例如:
Bill Punch 10 5
Jack Jab 7 7
Joe Kick 8 4
I would like to read the file and store its content.我想读取文件并存储其内容。 I could use an array, but I want to be able to continue to add rows.我可以使用数组,但我希望能够继续添加行。 So I've implemented a function to count the lines.所以我实现了一个函数来计算行数。 Arrays like to complain about values not being static at runtime so I moved to trying a vector.数组喜欢抱怨值在运行时不是静态的,所以我开始尝试使用向量。 Below is the code.下面是代码。
I'm attempting to insert every value into the x,y of the matrix, then go through and print it.我试图将每个值插入矩阵的 x,y 中,然后通过并打印它。 But it's not working;但它不起作用; it opens the command prompt and asks to input my file name.它打开命令提示符并要求输入我的文件名。 I do that, but then it just stops running.我这样做了,但随后它就停止运行了。
Also, how can I return the vector to my main
function?另外,如何将向量返回到我的main
函数?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// Global Variables
int numLines = 0;
string fileName;
int countLines();
string openFile();
int main() {
string name, attack;
cout << "What is the name of your data file?" << endl;
getline(cin, fileName);
cout << countLines() << endl;
cout << openFile() << endl;
}
int countLines() {
ifstream dataFile(fileName);
string line;
while (getline(dataFile, line)) {
numLines++;
}
return numLines;
}
string openFile() {
vector<vector<int>> dataVec(numLines, vector<int>(4));
ifstream dataFile(fileName);
string line;
while (getline(dataFile, line)) {
for (int i = 0; i == numLines - 1; i++) {
for (int o = 0; o == 4; o++) {
cin >> dataVec[i][o];
}
}
}
while (getline(dataFile, line)) {
for (int i = 0; i == numLines - 1; i++) {
for (int o = 0; o == 4; o++) {
cout << dataVec[i][o];
}
}
}
return dataVec;
}
There are better methods to read a CSV than you are using.有比您正在使用的更好的方法来读取 CSV。 Your method to read in a file of data are complicated and injecting defects.您读入数据文件的方法很复杂并且会注入缺陷。 Simplifying your algorithm can reduce the number of defects.简化算法可以减少缺陷的数量。
For example, using a structure to model the data row.例如,使用结构对数据行进行建模。 Let us call each line of data, a record.让我们称每一行数据为一条记录。
struct Record
{
std::string first_name;
std::string last_name;
int number1;
int number2;
};
You can store the records into a simple std::vector
:您可以将记录存储到一个简单的std::vector
:
std::vector<Record> database;
One method is to have the Record
read in its data members:一种方法是在其数据成员中读取Record
:
struct Record
{
std::string first_name;
std::string last_name;
int number1;
int number2;
friend istream& operator>>(istream& input, Record& r);
};
istream& operator>>(istream& input, Record& r)
{
input >> r.first_name;
input >> r.last_name;
input >> r.number1;
input >> r.number2;
return input;
};
This enables you to simplify your input loop:这使您能够简化输入循环:
Record r;
while (data_file >> r)
{
database.push_back(r);
}
If you need to know the number of lines in the file, you could use database.size()
.如果您需要知道文件中的行数,您可以使用database.size()
。 Otherwise, there is no need to know the number of lines in the file.否则,就不需要知道文件中的行数。
Similarly, you can write methods to print the record.同样,您可以编写方法来打印记录。 If you overload operator<<
, your print statement becomes:如果重载operator<<
,则打印语句变为:
cout << database[i] << endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.