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

这是我的代码如下所示:

#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++找到类似的另一个主题,但是找不到任何有用的东西。

最佳答案

目前尚不清楚您的问题到底出在什么地方,但是假设您要逐行读取文件并获取这些行的 vector ,则可以执行以下操作:

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;
}

10-05 18:44