读取文本文件并将每一行保存为字符串 (C++)

Reading a text file and saving every line as a string (C++)

我有一个小问题。我目前正在用 C++ 做一个学校作业,任务是有一个类似于小型图书馆的东西,用户可以要求看一本书,然后程序将打印出发行年份、作者、页数等等等。作业的重点是向量和数组,但我认为一个聪明的方法是将发布年份保存在文本文件中,然后将这些年份保存在数组中。当我第一次使用它时,所有内容都以字符形式保存(意思是“1”、“8”、“8”、“5”),而我实际上希望将文本文档中的每一行保存为字符串阵列,或类似的东西(如:“1885”,)。那时我真的不知道如何将它们拆分成字符串。然后我和一个朋友聊了一下,这就是我现在的代码,它并没有真正起作用,目前我不知道我应该如何解决它。 主要问题是我不知道如何读取文本文档中的每一行并将其保存为字符串,但是我非常感谢任何帮助我能够打印出文本文档中的一年,以任何其他方式。

我的代码是这样的:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <istream>

using namespace std;

void book();
void readFile(int input);
void oddEven();
void stringLiner();
void factorial();
int main()
{

int input;
while (input != 0)
{
cout << "Hello. Welcome to the first, truly big, assignment in this programming course." << endl;
cout << "Which part do you wish to access?" << endl;
cout << "1. Book program" << endl;
cout << "2. 2 arrays - One EVEN ~ One ODD" << endl;
cout << "3. The one at a time string" << endl;
cout << "4. Factorial array" << endl;
cout << "0. Exit " << endl;
cin >> input;
switch (input)
{
    case 1:
    book();
    break;

}


}
}

void book() //This is the function used to do the book thing
{
cout << string( 100, '\n' );

int input;

string year[5] = {"1883"/*Treasure Island*/ }; //Array for the years the  books were written
string author[5] = {"Robert Louis Stevenson"/*Treasure Island*/, "yollo"}; //Array for the authors
string pages[5] =  {"304"/*Treasure Island*/,"420" }; //Array for the number of pages
string books[5] = {"Treasure Island", "Swagolo" }; //Array for the name of the books
cout << "You have chosen to look at books." << endl;
cout << "These are the books in the library. Pick one to see what year it was written in, what author wrote it and how many pages it contains. " << endl;
cout << "These are the books in the library: " << endl;
for (int i = 0; i<5; i++)   //Loop to display all the books + what number to press to access them.
{
    cout << i+1 << " " << books[i] << endl;
};
cout << "Please type a number to look at that book. " << endl; 
cin >> input;
int TresIsl = input-1;
switch (input)  //Switch case to chose which book to look at.
{
    case 1: //Info about Treasure Island

    cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
    cout << books[TresIsl] << " was released in " ;
    readFile(input);
    cout << " and it was written by " << author[TresIsl] << ". ";
    cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
    break;

    case 2:
    cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
    cout << books[TresIsl] << " was released in " ;
    readFile(input);
    cout << " and it was written by " << author[TresIsl] << ". ";
    cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
    break;


}
}

void readFile(int input)
{
ifstream file("year.txt");
int numlines = 0;
int numMaxLines = 5;
vector<string> lines (numMaxLines);
while(numlines < numMaxLines && !file.eof())
{
    getline(file, lines);
    numlines++;
}
cout << lines[input];

}

其他 void 函数用于此作业中我现在没有包括的其他任务,我只是将代码复制粘贴到包括它们的位置。也请不要介意代码中略显幼稚的东西。

如果这违反了论坛或类似规则,我也很抱歉。我试图为 c++ 找到另一个类似的主题,但找不到任何有用的东西。

不清楚你的问题到底是什么,但假设你想读取一个文件 line-by-line 并获得这些行的矢量,这样就可以做到:

std::vector<std::string> readLines(const std::string& filename)
{
    std::vector<std::string> lines;
    std::ifstream input(filename);
    std::string line;
    while (std::getline(input, line))
    {
        lines.push_back(line);
    }
    return lines;
}

如果还有人有问题,我和一个朋友讨论过,他帮了我一点忙,我们得到了一个至少适用于我的情况的代码,所以我想我会把它展示给你看:

void readFile(int input)
{
ifstream file("year.txt");
string in;
vector<string> lines;
if (file.is_open())
{

    while ( getline (file, in) )
    {
        lines.push_back(in);
    }
    cout << in;

}
file.close();
cout<<lines[input-1]<<endl;
}

我猜最后的 cout 在某些情况下是不必要的,但这对我和我的作业有用。无论如何感谢大家的帮助。