繁体   English   中英

读取文本文件并将每一行另存为字符串(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 ++找到另一个类似的主题,但是找不到任何有用的东西。

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

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

最后,我认为在某些情况下不需要结尾,但这对我和我的家庭作业都是有效的。 无论如何,感谢大家的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM