繁体   English   中英

如何从文件读取长字符串到数组或类似的东西?

[英]how to read a long string from a file into an array or something similar?

所以我已经搜索了很长时间,没有发现任何能够解决我的问题的东西。我以前的问题有些混乱,有人可以尝试整理一下,以便它起作用还是直接重做,我有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();
    }
}

由于您想保留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;
}

注意:我尚未为您分配数组,因此您仍然需要自己执行此操作。

好吧,最后我自己解决这个问题实际上非常简单...因此,以防万一其他人遇到这个问题并且找不到简单的答案,我改为这样做。

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

暂无
暂无

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

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