简体   繁体   English

如何从C ++中的文本文件中获取特定行?

[英]How do I get specific lines from a text file in C++?

I need some help with C++ 我需要C ++的帮助

I am trying to create a program which contains excersises to practice the different German cases. 我正在尝试创建一个包含练习各种德国案例的练习的程序。

Hard-coding all questions and respective answers seems like an awful lot of work, and super inefficient. 对所有问题和相应答案进行硬编码似乎工作量很大,而且效率极低。

What I want my program to do, is: grab a random line from file X, and grab the same line number from file Y. (This seems like the easiest way to get both questions and answers from external files.) To me, it seems the most logical to get a random number, and use that as a line number. 我要我的程序做的是:从文件X抓取一条随机行,从文件Y抓取相同的行号。(这似乎是从外部文件中同时获得问题和答案的最简单方法。)对我来说,它似乎最合乎逻辑的方法是获取一个随机数,并将其用作行号。 But, that's about how far I got... 但是,那是我走了多远...

I know basic C++, but am very eager to learn. 我知道基本的C ++,但是非常渴望学习。

Can anyone please explain to me how to pull this off, including all necessary command? 谁能向我解释如何实现这一目标,包括所有必要的命令?

First, I would recommend that you store questions and answers in the same text file, probably by alternating between a question line and then an answer line. 首先,我建议您将问题和答案存储在同一文本文件中,这可能是通过在问题行和答案行之间交替来实现的。 This will make correcting mistakes, adding/removing questions, and general maintenance of your data easier. 这将使纠正错误,添加/删除问题以及数据的常规维护变得更加容易。

But if you want to keep them in separate files, the following code snippet will read your text file in and store the questions in an array (an stl vector) which you can then index or iterate any way you'd like: 但是,如果要将它们保存在单独的文件中,下面的代码段将读取您的文本文件,并将问题存储在数组(stl向量)中,然后您可以按任何方式对其进行索引或迭代:

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

int main()
{
        std::ifstream file("questions.txt");
        std::string line;
        std::vector<std::string> questions;
        while (std::getline(file, line))
        {
            questions.push_back( line );
        }
        // Now do something interesting with your questions. You can index them
        // like this: questions[5], or questions[random_index]
}

There are two ways of doing this: 有两种方法可以做到这一点:

  1. If you are planning on getting question/answer pairs you would be best to just read the who file line by line and store all the lines. 如果您打算获取问题/答案对,最好只逐行阅读who文件并存储所有行。 Then you just look it up in the array. 然后,您只需在数组中查找即可。

  2. If for some reason you only want to get one line at a time you'll have to read lines and count until you've gotten to the line you want. 如果由于某种原因,您一次只想获得一行,则必须阅读行数并计数,直到到达所需的行为止。

you may have a keyword for each line, like an id. 您可能在每一行都有一个关键字,例如ID。

that id can be paired to both questions, and answers if you have multiple files. 该ID可以同时与两个问题和答案配对(如果您有多个文件)。 or just pair the question, with the answer same order, or even same file. 或只是将问题与答案的顺序相同甚至文件配对。

You are constructing a database. 您正在构建数据库。
You should use a database. 您应该使用数据库。

The problem is that the question and answers are variable length records, which make positioning difficult. 问题是问题和答案是可变长度的记录,这使定位变得困难。 If all the records were the same length, you could position to a random record much faster. 如果所有记录的长度相同,则可以更快地定位到随机记录。

In order to find a text line, you will need to read past all the other newlines (since they are not in the same column in every line). 为了找到文本行,您需要阅读所有其他换行符(因为它们不在每一行的同一列中)。 This is fine if you only need to search once, but very slow to search many times. 如果您只需要搜索一次,但是搜索很多次却很慢,那很好。 Now comes the reason for the database. 现在介绍建立数据库的原因。

To make finding the questions and answers faster, create an index file or table. 为了更快地查找问题和答案,请创建索引文件或表。 (Starting to smell like a database). (开始闻起来像数据库)。 The index file will contain records of the form [question #, file position] where file position is the position in the question's file that the question starts on. 索引文件将包含[question #, file position]格式的记录,其中文件位置是问题在文件中开始该问题的位置。

You would load this file into memory and use it to index into the "questions" file. 您可以将该文件加载到内存中,并使用它来索引“问题”文件。 By storing the index contents into a file, you won't have to construct it from scratch each time your program starts; 通过将索引内容存储到文件中,您不必在程序每次启动时都从头开始构造索引。 only when the question's file changes. 仅当问题的文件更改时。

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

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