#### Archived

This topic is now archived and is closed to further replies.

# Need help w ifstream/ofstream

This topic is 5204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m soon having exam in basic c++. Because it is basic, I cannot use alot of fancy code on the exam. Keep that in mind. I have programmed c++ for a while, but I''m not an expert. In class I have learned loops, functions, arrays and char[] ( not string ), class, and a bit ifstream/ofstream. I have "not" learned pointers, and everything you normaly find after chapter 6 in a c++ book. I have never used ifstream/ofstream. I think I once used the fstream, or something else. I need help on interpeting the code, and some suggestions on improvements, but keep in mind ... no fancy code, cause "I have not learned that". I have, but I cannot use all my skills.
bool CPersons::WriteToFile()
{
ofstream outFile( "persons.dat" ); // creates persons.dat

outFile << nPersons << ''\n'';

for( int i = 0; i < nPersons; i++ )
{
outFile << array[ i ].nId << ''\n'';
outFile << array[ i ].strName << ''\n'';
outFile << array[ i ].bActive << ''\n'';
outFile << array[ i ].fBalance << ''\n'';
}
return true;
}

{
int i = 0;
ifstream inFile( "persons.dat" );

if( inFile )
{
inFile >> nPersons;
if( inFile.eof() )
{
cout << "file empty " << endl;
return false;
}
while( !inFile.eof() )
{
inFile >> array[ i ].nId;
inFile.ignore();
inFile.getline( array[ i ].strName, 50 );
inFile >> array[ i ].bActive;
inFile.ignore();
inFile >> array[ i ].fBalance;
inFile.ignore();
i++;
}
}
else
return false;
return true;
}

The WriteToFile(): It creates the person.dat ( overwrite it? ) - How can I test if the file is created, open and ready? nPerson is an int that keeps the total number of persons, so we know how many persons we are going to write/read to the file. I have an struct "data" with the 4 variables, nId, strName[], fBalance, bActive. Then I have created an "data array[2000]". Which I then write variable for variable to the file for each person ( data ) created. - I cannot pass the struct by struct to the file since I have not learned it. if everything goes well it returnes true. I was thinking of returning false if soemthing goes wrong, but how can I test if things goes wrong? The ReadFromFile(): It opens the persons.dat if( inFile ) returns true, then the file exist??? and is ok??? then we read the first post which tells us how many persons there are in our file. then we test if the end of file is reached...and return an error message + false. then we loop and read each post until we hit end of file... explain the cin, cin.getline, getline and the use of inFile.ignore() I''m not sure if I completly understand this. when you use cin, or read with >> then the ''\n'' remain in the stream, you remove ''\n'' with the ignore() so it doesn''t mess with the getline()... am I right??? so ignore() should always be used after cin, or >>? How can I improve my code?? keep it dead simple, C style, no pointers, no fstream.read() or how it was... I want to make the code more robust/sturdy/resilient ... not sure how to explain it... but if something wrong where to happen: file doesn''t exist, cannot read or write, mess with the data etc... I want to deal with those things... Sorry for this long post...

##### Share on other sites
I have "not" learned pointers, and everything you normaly find after chapter 6 in a c++ book.

Not all books are the same.

I have never used ifstream/ofstream. I think I once used the fstream, or something else.

Are you talking about the old iostream library or the standard iostream library ?

ifstream and ofstream are similar to fstream, except they only do input (ifstream) or output (ofstream).

The WriteToFile():
It creates the person.dat ( overwrite it? )
- How can I test if the file is created, open and ready?

if (outfile) { /* the file is created, open and ready */ }

- I cannot pass the struct by struct to the file since I have not learned it.

I can''t understand you. The answer shall therefore be "learn it".

if everything goes well it returnes true. I was thinking of returning false if soemthing goes wrong, but how can I test if things goes wrong?

Depends on what goes wrong. Places to start : outfile.good(), outfile.bad(), outfile.fail(), outfile.eof(), and treating the stream object as a bool ( if (outfile), if (!outfile)).

if( inFile ) returns true, then the file exist??? and is ok???

Yes.

explain the cin, cin.getline, getline and the use of inFile.ignore()

cin is the standard input stream. It is an object, not a function.

cin.getline(a,b) reads a line of at most b-1 characters into a char array a.
The std::getline function acts similarly with C++ strings.
inFile.ignore() skips one character.

so ignore() should always be used after cin, or >>?

No. Only if it does what you need to do. operator>> will automatically skip whitespace before reading. Do not rely on principles like "You should always do X", learn what X means instead.

std::ws will eat any amount of whitespace.

How can I improve my code?? keep it dead simple, C style, no pointers, no fstream.read() or how it was...

That''s too vague a question and too strong a requirement.
"C-style, no pointers" is an oxymoron.
You''re doing formatted I/O, you don''t need fstream.read().

I want to make the code more robust/sturdy/resilient ... not sure how to explain it...

Error-checking. See above.

file doesn''t exist

The stream object will evaluate to false.
stream.is_open() will be false.
Study the iostream documentation.

stream.fail() will be true after the failed operation.
Study the iostream documentation.

mess with the data

A failed operation does not modify the data.
For anything else, you have to take your own precautions.