Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fstreams and i/o 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello all,

I'm a beginner w/ C++ so please bear with me. I am creating a file to which I have to do frequent i/o (it's an index).
I thought that I should do this:

fstream fs;
fs.open(filename, ios::in|ios::eek:ut|ios::binary);

but I find that I get fs.good() == 0 unless I omit the ios::in. When I have

fstream fs;
fs.open(filename, ios::eek:ut|ios::binary);

I can write to, but not read from the file properly. Should I even be able to, considering that the stream is not being opened for input? How can I get around this? (I tried opening and closing the stream in the different modes, ios::in or ios::eek:ut, depending on what I needed, but that doesn't seem to work).

TIA for any insight you can give! :)
 
You're opening the file the wright way,strange it doesn't work.Is the file you want to read from empty? Have you tested it with other tests dan good() ?
e.g. eof(),fail(),bad()?
When do you test on good(), directly after you open the file ?
Normally what you do should work (I suppose you do use the seekp function to move in the file)

I'll give you a test program that works:

#include <iostream.h>
#include <fstream.h>

void main(){

fstream file(&quot;test.txt&quot;,ios::in|ios::eek:ut);
file<<110<<endl;
file<<&quot;hundered and ten&quot;<<endl;

file.seekp(0); //the put pointer
file<<111;

file.seekg(0); //the get pointer
int number;
char txt[80];

file>>number;
file>>txt;

cout<<number<<endl<<txt;
cin.get();
}

Hope this helps X-)

Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top