【问题标题】:Why is the inner loop for parsing the line I read being skipped every other iteration through the outter reading loop?为什么解析我读取的行的内部循环通过外部读取循环每隔一次迭代都被跳过?
【发布时间】:2015-04-07 03:09:18
【问题描述】:
#include <iostream>  
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

int main(int argc, char* argv[])
{;
    int arraylen = 7, arraylen_new = 0, i = 0, nextstep = 0;
    string line, field, junkText;
    string* randNum = new string[arraylen];
    string* phrase = new string[arraylen];

    ifstream randomTxt;
    arraylen = 5;

    randomTxt.open("random.txt");

    while(!randomTxt.eof())
    {
        getline(randomTxt,line);
        stringstream ss(line);
        nextstep = 0;
        while(getline(ss,field,','))
        {
            cout << i << endl;
            /*if(nextstep == 0)
            {
                randNum[i] = field;
                cout<< "number " << i << ": " << randNum[i] << endl;
                nextstep = 1;
            }

            else if(nextstep == 1)
            {
                junkText = field;
                cout<< "junk " << i << ": " << junkText << endl;
                nextstep = 2;               
            }
            else
            {
                items[i] = field;
                cout<< "items " << i << ": " << items[i] << endl;
            }*/

            if(i == (arraylen-1))
            {
                arraylen_new = arraylen + 1;
                string* temp1 = new string[arraylen_new];
                string* temp2 = new string[arraylen_new];
                copy(randNum, (randNum+arraylen), temp1);
                delete [] randNum;
                randNum = temp1;

                copy(phrase, (phrase + arraylen), temp2);
                delete [] phrase;
                phrase = temp2;
                arraylen = arraylen_new;
            }
        }
        cout<<"test"<<endl;
        i++;

    }    
    randomTxt.close();

    /*for(int i = 0; i < arraylen;i++)
    {
        cout<< "UPC " << i << ": " << UPC[i] << "\t" << "Phrase " << i << ": " << phrase[i] << endl;

    }*/

    return 0;
}

检查i 的值时,打印输出如下:

0
0
0
test
test
2
2
2
test
test
4
4
4

依此类推,直到我的文件被完全读取。我的迭代需要正确匹配,因为我使用它将每一行保存到单独的数组中。任何想法为什么它似乎跳过了内部 while 循环?

这是文本文件的sn-p:

333331234567,blah,"this is nonsense"
222221234567,word,"yadda yadda words"
111111234567,thing,"total gibberish stuff"

【问题讨论】:

  • @NathanOliver 好的,我的帖子已被编辑以包含链接中的代码。很抱歉造成混乱。
  • 在修复了很多错误的语句(但没有逻辑)之后,你的代码对我来说是正确的。你确定这是正确的代码和文本文件吗?
  • 免费建议:获取有关 RAII 的现代 C++ 书籍,忘记string* randNum = new string[arraylen]。替换为std::vector&lt;std::string&gt; randNum(arraylen),您将节省大量时间。
  • 我仅限于数组,所以没有向量。
  • This doesn't come close to compiling。还请删除任何不添加信息的 cmets,以便我们留下 minimal complete verifiable example

标签: c++ file-io while-loop


【解决方案1】:

我不会重现您的问题。

但我必须在您的示例代码中修复一些名称,所以也许我不小心修复了它?

这是我的版本,带有一些额外的 DEBUG 打印语句。

还请注意,检查eof()很少有用,因此将外部循环更改为while (getline(randomTxt, line))

然后有了i循环索引,我认为写成for循环更自然。

Live On Coliru

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

/// returns: new array length

int main() {
    size_t arraylen = 7;
    std::string line;
    std::string *randNum = new std::string[arraylen];
    std::string *phrase  = new std::string[arraylen];

    std::ifstream randomTxt;

    randomTxt.open("random.txt");

    for (size_t i=0; getline(randomTxt, line); ++i)
    {
        std::cout << "DEBUG LINE: '" << line << "'\n";
        //nextstep = 0;

        std::stringstream ss(line);
        std::string field;
        while (getline(ss, field, ',')) {
            std::cout << "DEBUG INNER " << i << " '" << field << "'\n";

            if (i == (arraylen - 1)) {
                size_t arraylen_new = arraylen + 1;
                {
                    std::string *tmp = new std::string[arraylen_new];
                    copy(randNum, randNum + arraylen, tmp);
                    delete[] randNum;
                    randNum = tmp;
                }

                {
                    std::string *tmp = new std::string[arraylen_new];
                    copy(phrase, (phrase + arraylen), tmp);
                    delete[] phrase;
                    phrase = tmp;
                }
                arraylen = arraylen_new;
            }
        }
    }
}

打印

DEBUG LINE: '333331234567,blah,"this is nonsense"'
DEBUG INNER 0 '333331234567'
DEBUG INNER 0 'blah'
DEBUG INNER 0 '"this is nonsense"'
DEBUG LINE: '222221234567,word,"yadda yadda words"'
DEBUG INNER 1 '222221234567'
DEBUG INNER 1 'word'
DEBUG INNER 1 '"yadda yadda words"'
DEBUG LINE: '111111234567,thing,"total gibberish stuff"'
DEBUG INNER 2 '111111234567'
DEBUG INNER 2 'thing'
DEBUG INNER 2 '"total gibberish stuff"'

注意事项

  • 您可能需要创建一个 grow_array 函数,因为您复制了该代码
  • 您可能希望按因子增长,而不是一次增长 1 个元素;这根本不会扩展并导致糟糕的性能/堆碎片

【讨论】:

  • 谢谢你,今天晚些时候我会去看看。我还计划像你建议的那样制作一个功能,这只是我项目的“草稿”。一旦我得到这个工作,我会把它分成自己的功能。
  • Live On Coliru(固定链接)
  • 我用 for 循环尝试过,也遇到了同样的问题。在我将解析行的每一段保存到单独的数组的注释部分中是否存在问题?
  • 没有,因为 cmets 没有被执行。此外,您声称内部循环没有运行,这意味着即使没有注释,也会跳过此代码(专业提示:分享 实际 代码。这很烦人浪费时间让我们猜测)
  • 我编辑了我的代码以确保它按原样运行。如果您现在使用它并运行它,输出应该会显示我遇到的重复问题。即使将 while 循环更改为 for 循环也有同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
相关资源
最近更新 更多