我正在尝试读取文件,将每一行添加到向量中,然后打印向量。 但是现在,它只会打印第一行。因此,我假设第一行是添加到向量中的唯一行,但是我不知道为什么。
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::vector<std::string> vecOfStrs;
std::ifstream fileIn("example.txt");
std::string str;
std::string newLine;
newLine = str + "\n";
while (std::getline(fileIn,str)) {
std::string newLine;
newLine = str + "\n";
if (newLine.size() > 0) {
vecOfStrs.push_back(newLine);
}
fileIn.close();
for (int i = 0; i < vecOfStrs.size(); i++) {
std::cout << vecOfStrs.at(i) << ' ';
}
}
}
这是文本文件,现在它应该完全按照此处显示的格式打印出来:
Barry Sanders
1516 1319 1108 1875 -999
Emmitt Smith
1892 1333 1739 1922 1913 1733 -999
Walter Payton
1999 1827 1725 1677 -999