简体   繁体   English

在同一fstream对象C ++上使用getline的两个函数

[英]Two functions working with getline on same fstream object c++

I have the following problem. 我有以下问题。 I have these two functions. 我有这两个功能。 The first one is reading contact information from a txt file, contacts separated by the symbol '#' like this- 第一种是从txt文件中读取联系人信息,联系人之间用符号“#”分隔,如下所示:

//sample txt file
#1
Name: AAA
Phone: 08782634
Phone: 0245637
Date: 23.34
Phone: 324324324
#2
Name: BBB
Phone: 99999

and it finds the length of each contact(number of lines between every '#'). 并找到每个触点的长度(每个“#”之间的行数)。 The second one calls the first one and then the contact should be printed, but it prints the second contact, not the first. 第二个呼叫第一个,然后应打印该联系人,但它将打印第二个联系人,而不是第一个。

Is it possible that getline from the first function changes the stream somehow, because the second function works perfectly when I use it without the first (hardcored capacity to a const int)? 第一个函数的getline是否有可能以某种方式更改流,因为当我在没有第一个函数的情况下使用第二个函数时,第二个函数可以完美地工作(对const int的硬核容量)?

int Contact::FindNumberOfFields(std::ifstream& in)
{
    char* buffer = new char [1024];
    int cnt = 0;
    int i = 0;
    int pos = 0;
    while(in)
    {
        in.getline(buffer, 1024);
        if (strchr(buffer, '#'))
        {
            while (in.getline(buffer, 1024))
            {
                if (!strchr(buffer, '#') && strlen(buffer))
                {
                    cnt++;
                }
                else
                {
                    return cnt;
                }
            }


        }
    }
    in.clear();
    in.close();
    delete [] buffer;
}

void Contact::ReadContactFromStream(std::ifstream& in)
{
    SetCapacity(FindNumberOfFields(in));
    // cout << GetCapacity() << endl; // output is five, correct
    while(in)
    {
        if (addedFields >= GetCapacity()) // works correct when addedFields >= hardcored int (5) and removing SetCapacity(in) from code
        {
            break;
        }
        contactTypes[addedFields] = CreateObjectFromLine(in);
        addedFields++;
    }

}

All files have a "current read position", and as you read from the file that position is advanced. 所有文件都有一个“当前读取位置”,并且当您从文件中读取该位置时,该位置将前进。 Unless you change the position you will always read the next thing in the file, which at the moment "#2" is the next record. 除非更改位置,否则您将始终读取文件中的下一个内容,此刻"#2"是下一个记录。

My suggestion for you to solve it, is to simply have one function which reads data, and when it comes to a new record marker then it initializes an empty vector of strings, and read the lines of the record into this vector, and then pass this vector on to a function which parses the contents. 我的建议为你解决它,是简单地有一个函数读取数据,而当它涉及到一个新的记录标记,然后它初始化一个空的向量的字符串,并读取记录的行到该载体,然后通过将此向量传递给解析内容的函数。

Something like the following pseudo code: 类似于以下伪代码:

std::getline(stream, line);
for (;;)
{
    if (line is start of record)
    {
        std::vector<std::string> current_record;
        while (std::getline(stream, line))
        {
            if (line is start of record)
                break;  // Break out of inner loop
            else
                current_record.push_back(line);
        }

        parse_record(current_record);
    }
}

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

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