【问题标题】:how to read a long string from a file into an array or something similar?如何将文件中的长字符串读入数组或类似的东西?
【发布时间】:2015-01-05 22:17:40
【问题描述】:

所以我已经搜索了很长一段时间,但目前没有发现任何可以解决我的问题的问题 我之前的问题中有一个乱七八糟的代码,有人可以尝试整理一下以使其正常工作或简单地重新制作它,我有 4 个不同的文本要读取的文件取决于它是什么,所以 switch 语句是我最好的主意,我打算保留这一点。仅仅添加这段代码是行不通的......而且无法弄清楚为什么不这样做。

ifstream QuestionFile;
int i = 0;
switch (x){
case 1:
    QuestionFile.open("Topic1 Questions.txt", ios::app);
    break;
case 2:
    QuestionFile.open("Topic2 Questions.txt", ios::app);
    break;
case 3:
    QuestionFile.open("Topic3 Questions.txt", ios::app);
    break;
case 4:
    QuestionFile.open("Topic4 Questions.txt", ios::app);
    break;
}
stringstream buffer;
buffer << QuestionFile.rdbuf();
string test = buffer.str();

size_t pos1 = 0;
size_t pos2;

if (!QuestionFile)
{
    cout << "Cannot load file" << endl;
}
else
{
    if (QuestionFile.peek() != ifstream::traits_type::eof()) {
        while (!QuestionFile.eof())
        {
            pos2 = test.find("|", pos1);
            questions[i] = test.substr(pos1, (pos2 - pos1));
            cout << questions[i];
            i++;
        }
        QuestionFile.close();
    }
}

【问题讨论】:

  • 你的编程语言是什么?
  • 在我看来像 C++,它被标记为这样。
  • 我将它标记为 C++,所以 helloflash 看不到该标记。

标签: c++ arrays string file


【解决方案1】:

既然你想保留 switch 语句,试试这个:

const char   *filename;
size_t       pos;
size_t       oldPos;
stringstream buffer;
string       test;

filename = NULL;
switch(x)
{
  case 0:
    filename = "Topic1 Questions.txt";
    break;
  case 1:
    filename = "Topic2 Questions.txt";
    break;
  case 2:
    filename = "Topic3 Questions.txt";
    break;
  case 3:
    filename = "Topic4 Questions.txt";
    break;
}

QuestionFile.open(filename, ios::app);   // try opening the file
if(QuestionFile.is_open())               // if open succeeds...
{
    buffer << QuestionFile.rdbuf();
    test = buffer.str();                 // read entire file into 'test'
    pos = 0;                             // start at beginning
    do
    {
        oldPos = pos;
        pos = test.find("|", oldPos);    // find position of next line
        questions[i] = test.substr(oldPos, (pos - oldPos));  // put in array
        i++;
    }
    while(pos != oldPos);  // as long as pos2 changes.
    QuestionFile.close();
}
else
{
    cout << "Cannot load file" << endl;
}

注意:我没有为你分配了一个数组,所以你仍然需要自己做。

【讨论】:

  • 这是一个任务,这是我这样做的一般方式,因此避免让它看起来与我知道如何做或应该知道的不同,因此我说我想保留 switch 语句,因为它应该可以正常工作并且似乎可以。
  • 作为一般规则,我建议不要在 switch 语句中打开文件,因为它会破坏程序结构(请参阅通常如何读取文件的流程示例)。 - 此外,您似乎从阅读整个文件开始,然后检查是否还有更多要阅读的内容,然后尝试阅读这些行。你永远不会进入那部分代码。
  • 抱歉,第二点对我来说没有多大意义。
  • 我已经重写了我的答案(当你发布你的答案时)。它可能仍然有用,因为我相信它的某些部分仍然相关;特别是打开文件一次,测试文件是否打开和关闭文件,这在您的解决方案中并不总是发生。
【解决方案2】:

好的,所以我自己最终解决了这个问题实际上非常简单......所以以防万一其他人遇到这个并且找不到简单的答案,我已经这样做了。

ifstream QuestionFile;
int i = 0;
//switch statement for checking which text file I personally wanted to use as it 
//depending on which the user was allowed to use.
switch (x){
case 1:
    QuestionFile.open("Topic1 Questions.txt", ios::app);
    break;
case 2:
    QuestionFile.open("Topic2 Questions.txt", ios::app);
    break;
case 3:
    QuestionFile.open("Topic3 Questions.txt", ios::app);
    break;
case 4:
    QuestionFile.open("Topic4 Questions.txt", ios::app);
    break;
}
if (!QuestionFile)
{
    cout << "Cannot load file" << endl;
}
else
{
    if (QuestionFile.peek() != ifstream::traits_type::eof()) {
        while (!QuestionFile.eof())
        {
            //This simply gets which ever line you want and puts it into that part of the array
            //and does this till the end of the array.
            getline(QuestionFile, questions[i]);
            i++;
        }
        QuestionFile.close();
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 2017-10-16
    • 2014-02-28
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2011-03-02
    相关资源
    最近更新 更多