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

时间:2016-01-13 09:11:07

标签: c++ arrays ifstream

我有一个小问题。我目前正在用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 ++找到这样的另一个话题,但我找不到任何有用的东西。

2 个答案:

答案 0 :(得分:4)

目前还不清楚你的问题到底是什么,但假设你想逐行读取文件并得到这些行的向量,这样就可以了:

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

答案 1 :(得分:0)

如果还有人提出问题,我和朋友讨论过这个问题并帮助了我一点,我们得到的代码至少在我的案例中有效,所以我想我会表现出来它给你:

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在某些情况下是不必要的,但这对我和我的作业都很有用。感谢大家的帮助。