stream fail flag help

Oct 30, 2014 at 6:41pm
I'm trying to check the status of my file i/o stream in an if statement using the fail() function and I get a build error. I've tried to sift through the reference articles here but I'm terribly confused on the topic of flags. Can anyone help me?
(I'm using Codeblocks)
Oct 30, 2014 at 7:12pm
Can't help if you don't post the error you're getting.
We're not mind readers.
Oct 30, 2014 at 10:16pm
Sorry I'm not used to forums. I'll get on it
Oct 30, 2014 at 10:21pm
reference to ios_base is ambiguous
candidates are class ios_base
class std::ios_base
'ios_base' does not name a type
'fail' was not declared in this scope

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <fstream>
#include <istream>
#include <ostream>

using namespace std;

class ios_base;


1
2
3
4
5
6
7
8
9
10
11
if(fail() == true)
    {
        NewUserCount = 0;
    }
    else
    {
        NewUserCount++;
        NewUser << NewUserCount;
    }
    NewUser.close();
Oct 31, 2014 at 2:51am
class ios_base;
Try removing this (line 11).
Oct 31, 2014 at 7:54pm
Please pardon me I left a line out
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <fstream>
#include <istream>
#include <ostream>

using namespace std;

class ios_base;
ios_base::setiosflags;
Nov 3, 2014 at 9:42pm
bump
Nov 3, 2014 at 10:04pm
closed account (SECMoG1T)
Hi
class ios_base;/// tremove this you are attempting to redefine a library class]

if(fail() == true) what stream are checking here ?

1
2
3
4
5
6
7
8
9
10
//if ifile is a file stream
ifstream ifile ("data.txt");
///check like this
 if (ifile. fail ()){//do something}

///even simpler
 if (! ifile)//better

///evaluate on read
while (ifile)//get data from file 

Last edited on Nov 3, 2014 at 10:05pm
Nov 3, 2014 at 10:08pm
Please explain it to me as though I were five because you've lost me.
Nov 3, 2014 at 10:35pm
closed account (SECMoG1T)
Sorry for that

If a stream in ok then a goodbit is set on that stream meaning you can use that stream
To read

If a stream runs into an error you cant use that stream
We always check to see if an error state have been set on a stream

Three error states can be set

1. Failbit - this set when a stream runs into a resolvable eg read the wrong data type for example
1
2
3
4
5
 int val; cin>> val ; /// suppose you key in char 'z' to val a Failbit will be set on that stream
         ///and the following reads will fail , you might experience a scenario similar to an infinite loop
        /// this error state can be cleared by using clear () that each stream have
       cin.clear ();  stream can then be used again
   


2. eofbit- this error state is set when a stream strikes the end-of-file character

3. badbit- this is set when the stream reads corrupt data this stream can't be reused again

By default the goodbit state is always set if stream is ok

while (!ifile)///is a Boolean expression it checks if any of the error state is sey

///note all those States apply to iostreams filestreams and stringstreams
Last edited on Nov 3, 2014 at 10:39pm
Nov 3, 2014 at 10:37pm
How would I check the error state with the loop?
Last edited on Nov 3, 2014 at 10:43pm
Nov 3, 2014 at 10:59pm
closed account (SECMoG1T)
Ley say you have a text file in which there are multiple friend names per line, you can use the loop to read all names while no error state is set om the stream

1
2
3
4
5
6
7
8
9
10
ifstream ifile ("names.txt");

if(! ifile){cout <<"file not found"; exit (1);}// if goodbit isn't set because the file wasn't found exit;
string name;

while (ifile)///otherwise while goodbit is on read names
 {
    getline (ifile, name,'\n');
    cout <<name <<endl; 
 }
Nov 3, 2014 at 11:05pm
Thanks mate
Nov 3, 2014 at 11:12pm
one more thing: is it possible to write an integer to a file?
Nov 4, 2014 at 6:54am
closed account (SECMoG1T)
You can write any data type you want
Topic archived. No new replies allowed.