Reputation: 3
I'm trying to do a bunch of stuff with the .txt file I'm trying to read, so I want to break it up into functions. But even when I pass the file stream in by reference, I can't get the program to compile.
#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; }
}
}
Upvotes: 0
Views: 163
Reputation: 3003
To resolve your problem you can just remove const
keyword in declaration of findStart
funciton.
TL;DR;
in generally if you want to only read from file, please use ifstream instead of fstream.
your code problem is stream >> temp;
does not work with const fstream
because operator >>
has declared like below
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 );
as you can see operator>>
does not have any overload for const reference
stream object, so your code is wrong and does not compile, if you want to know why C++ does not provide this override you can see below implementation for example
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 }
as you can see in above example, for implementing operator>>
we need to change state of stream to know (and save) last read position.
Upvotes: 1