簡體   English   中英

C++ fstream 對象作為參考傳遞,但它不會

[英]C++ fstream object passed as reference, but it won't make

我試圖用我試圖閱讀的 .txt 文件做很多事情,所以我想把它分解成函數。 但即使我通過引用傳遞文件流,我也無法編譯程序。

    #include "Executive.h"
    #include "Clip.h"
    #include <string>
    #include <iostream>
    #include <fstream>

    void Executive::readFile()
    {
        std::fstream streamer;
        streamer.open(m_file);
        if(streamer.is_open ())
        {
            findStart(streamer);

            for(int i = 0; i < 13; i++)
            {
                std::string temp;
                streamer >> temp;
            }

            for(int i = 0; i < 20; i++)
            {
                std::string temp;
                streamer >> temp;
                std::cout << temp << "  ";
                if(i == 10) {std::cout << "\n";}
            }

            streamer.close();
            return;
        }
        else
        { throw std::runtime_error("Could not read file!\n"); }
    }

    void findStart(const std::fstream& stream)
    {
        bool isStart = 0;

            while(!isStart)
            {
                std::string temp;
                stream >> temp;

                if(temp == "Sc/Tk")
                { isStart = 1; }
            }
    }

ITNOA

簡單的答案

為了解決您的問題,您可以在findStart函數的聲明中刪除const關鍵字。

TL;博士;

一般來說,如果您只想從文件中讀取,請使用ifstream而不是 fstream。

您的代碼問題是stream >> temp; 不適用於const fstream ,因為operator >>已聲明如下

template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT, Traits>&
    operator>>( std::basic_istream<CharT, Traits>& is,
                std::basic_string<CharT, Traits, Allocator>& str );

如您所見, operator>>沒有任何const reference流對象的重載,因此您的代碼是錯誤的並且無法編譯,如果您想知道為什么 C++ 不提供此覆蓋,您可以查看下面的實現,例如

1540     {
1541       typedef basic_istream<_CharT, _Traits>            __istream_type;
1542       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
1543       typedef typename __istream_type::ios_base         __ios_base;
1544       typedef typename __istream_type::int_type         __int_type;
1545       typedef typename __string_type::size_type         __size_type;
1546
1547       __size_type __extracted = 0;
1548       const __size_type __n = __str.max_size();
1549       typename __ios_base::iostate __err = __ios_base::goodbit;
1550       typename __istream_type::sentry __cerb(__in, true);
1551       if (__cerb)
1552         {
1553           __try
1554             {
1555               __str.erase();
1556               const __int_type __idelim = _Traits::to_int_type(__delim);
1557               const __int_type __eof = _Traits::eof();
1558               __int_type __c = __in.rdbuf()->sgetc();
1559
1560               while (__extracted < __n
1561                      && !_Traits::eq_int_type(__c, __eof)
1562                      && !_Traits::eq_int_type(__c, __idelim))
1563                 {
1564                   __str += _Traits::to_char_type(__c);
1565                   ++__extracted;
1566                   __c = __in.rdbuf()->snextc();
1567                 }
1568
1569               if (_Traits::eq_int_type(__c, __eof))
1570                 __err |= __ios_base::eofbit;
1571               else if (_Traits::eq_int_type(__c, __idelim))
1572                 {
1573                   ++__extracted;
1574                   __in.rdbuf()->sbumpc();
1575                 }
1576               else
1577                 __err |= __ios_base::failbit;
1578             }
1579           __catch(__cxxabiv1::__forced_unwind&)
1580             {
1581               __in._M_setstate(__ios_base::badbit);
1582               __throw_exception_again;
1583             }
1584           __catch(...)
1585             {
1586               // _GLIBCXX_RESOLVE_LIB_DEFECTS
1587               // 91. Description of operator>> and getline() for string<>
1588               // might cause endless loop
1589               __in._M_setstate(__ios_base::badbit);
1590             }
1591         }
1592       if (!__extracted)
1593         __err |= __ios_base::failbit;
1594       if (__err)
1595         __in.setstate(__err);
1596       return __in;
1597     }

如您在上面的示例中所見,為了實現operator>> ,我們需要更改流的狀態以了解(並保存)上次讀取位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM