tags:

views:

690

answers:

3

I think i might need to use a boolean bValue = false for my while condition:

char cArray[ 100 ] = "";
ifstream object;
cout << "Enter full path name: ";
cin.getline( cArray, 100 );
if ( !object ) return -1   // Path is not valid? This statement executes why?

ifstream object.open( cArray, 100 );

// read the contents of a text file until we hit eof.
while ( !object.eof() )
{
// parse the file here

}

Why can i not enter the full path name of the text file?

It might be because of the eof. Is their syntax for a boolean statement that can emulate eof?

Can i have:

while ( !object == true )
{
// parase contents of file
}
A: 

Does this even compile? "ifstream object.open(...)" isn't valid syntax. Also, is there a reason you're using a char array instead of a std::string for the input value?

Also, why are you checking object's status before you do anything with it?

Joe
A: 

The ifstream::open function takes a filename and an optional mode. Since you want to read the whole file, why not just start at the begining:

ifstream obj(cArray);
if (obj) { // file successfully opened for reading
    while (obj.good()) {
        // read in a line at a time
        string line;
        getline(line, obj);
        if (!line.empty()) { // we have something to work with
           // parse
        }
    }
}

Of course, a sleeker version is to test for getline in the while loop as Neil Butterworth.

dirkgently
I guess i just can not read in the whole file path..There actually might not even be a problem with the eof
@lampshade: Check out if your buffer cArray is large enough for the file path or not.
dirkgently
this really isn't a good idea - "parse" will be called even if getline couldn't read anything
anon
@Neil: Of course, this isn't the production quality but just to give an idea.
dirkgently
@dirk - I think we should encourage people to realise that they must test if getline (or whatever read function they use) has actually read something
anon
+4  A: 

Please will you and everyone else note that the correct way to read a text file does NOT require the use of the eof(), good(), bad() or indifferent() functions (OK, I made the last one up). The same is true in C (with fgets(), feof() et al). Basically, these flags will only be set AFTER you have attempted to read something, with a function like getline(). It is much simpler and more likely to be correct to test that read functions, like getline() have actually read something directly.

Not tested - I'm upgrading my compiler:

#include <iostream>
#include <fstream>
#include <string>
using namespacr std;

imt main() {

   string filename;
   getline( cin, filename );

   ifstream ifs( filename.c_str() );
   if ( ! ifs.is_open() ) {
       // error
   }

   string line;
   while( getline( ifs, line ) ) {
       // do something with line
   }
}
anon
can i use a senteinal controlled while condition then? Insted of the your getline( ifs, line )?
I don't understand what you mean by "senteinal"
anon
http://en.wikipedia.org/wiki/Sentinel_value
Dustin Getz