【问题标题】:Reading a text file and saving every line as a string (C++)读取文本文件并将每一行保存为字符串 (C++)
【发布时间】:2016-01-13 09:11:06
【问题描述】:

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

【问题讨论】:

  • 不是问题的根源,而是your reading loop is faulty
  • 你已经比大约 75% 的首次发帖人做得更好。你已经向我们展示了你到目前为止所获得的,甚至是一个完整的程序。
  • 我怀疑您最近介绍了结构化数据(结构和类),并且希望您通过使用名为“Book”的结构将其应用于此问题。但这只是一种预感。
  • @molbdnilo 在这之后我们实际上正在上课......
  • @KungKranium 这是一个有趣的方法。在这种情况下,如果你得到与这个非常相似的任务,请不要感到惊讶。 (我有点赞成“故意造成问题,以便以后证明为什么 X 是一件好事”的方法,但它可能会导致这里的相关答案不太相关。)

标签: c++ arrays ifstream


【解决方案1】:

目前尚不清楚您的问题到底是什么,但假设您想逐行读取文件并获取这些行的向量,则可以这样做:

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

【讨论】:

  • 那么你写std::vector&lt;std::string&gt; years = readLines("years.txt");而不是写string years[5] = {...};。 (养成不写using namespace std的习惯。)实际上,我会写成const auto years = readLines("years.txt");
  • 请问我为什么要使用using namespace std ?我的老师告诉我要使用它,所以我想我从来没有质疑过它。
  • @KungKranium This question/answer 回答了这个问题。
  • @KungKranium 您的老师可能最想为您节省一些繁琐的打字。在像这样的小程序中,这并不重要。
【解决方案2】:

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

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 是不必要的,但这对我和我的作业都有效。无论如何感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多