运用ifstream的getline时需要注意的问题

    我们在应用中经常会遇到从文件中读行的操作,

当然方法有多种,这里我想和大家探讨一下用ifstream的getline方法:

主要code如下:

……

    const int MAXLEN = 256;

    std::vector vecFilelines;

    std::ifstream fs("test.txt");

    if (fs.good())

    {

        char    stringline[MAXLEN];

        while (!fs.eof())

        {

            fs.getline(stringline, MAXLEN);

            vecFilelines.push_back(stringline);

        }

    }

……

 

初步看一下上面的代码没什么问题,

如果你所要读的每一行的长度不超过255,程序运行也不会有问题

 

但当你所要读的长度超过了255,就有问题了,我尝试的现象是:

当读到第一个超过255的行时,程序会陷入死循环,内存会被慢慢耗尽。

 

经过对istream里getline方法的分析,找出原因如下:

 

……

    _Myt& __CLR_OR_THIS_CALL getline(_Elem *_Str,

        streamsize _Count, _Elem _Delim)

        {   // get up to _Count characters into NTCS, discard _Delim

        _DEBUG_POINTER(_Str);

        ios_base::iostate _State = ios_base::goodbit;

        _Chcount = 0;

        const sentry _Ok(*this, true);

 

        if (_Ok && 0 < _Count)

            {   // state okay, use facet to extract

            int_type _Metadelim = _Traits::to_int_type(_Delim);

 

            _TRY_IO_BEGIN

            int_type _Meta = _Myios::rdbuf()->sgetc();

 

            for (; ; _Meta = _Myios::rdbuf()->snextc())

                if (_Traits::eq_int_type(_Traits::eof(), _Meta))

                    {   // end of file, quit

                    _State |= ios_base::eofbit;

                    break;

                    }

                else if (_Meta == _Metadelim)

                    {   // got a delimiter, discard it and quit

                    ++_Chcount;

                    _Myios::rdbuf()->sbumpc();

                    break;

                    }

                else if (--_Count <= 0)

                    {   // buffer full, quit

                    _State |= ios_base::failbit;

                    break;

                    }

                else

                    {   // got a character, add it to string

                    ++_Chcount;

                    *_Str++ = _Traits::to_char_type(_Meta);

                    }

            _CATCH_IO_END

            }

 

        *_Str = _Elem();    // add terminating null character

        _Myios::setstate(_Chcount == 0 ? _State | ios_base::failbit : _State);

        return (*this);

        }

……

 

当你读到一个大于255的行时,由逻辑

                else if (--_Count <= 0)

                    {   // buffer full, quit

                    _State |= ios_base::failbit;

                    break;

                    }

会将文件的_State置为ios_base::failbit从而_Ok变成false

而后再继续getline时,_Ok将会一直为false,而getline会一直返回空串,在while中陷入死循环。

 

 

改善如下:

……

    const int MAXLEN = 256;

    std::vector vecFilelines;

    std::ifstream fs("test.txt");

    if (fs.good())

    {

        char    stringline[MAXLEN];

        while (!fs.eof())

        {

            if (fs.getline(stringline, MAXLEN).good())

{

                vecFilelines.push_back(stringline);

            }

            else

            {

                // add you logic to deal with the unexpected situation

                break;

            }

        }

    }

……

 

如有不对之处,还请高手及时指出,谢谢!


你可能感兴趣的:(c/c++)