|
-
March 29th, 2004, 04:02 PM
#1
how to read from text file
Hey guys,
I'm having trouble reading an integer from a text file.
for example I have the following stored in a text file
------------ beginning of file------------excluding this line
H
88
------------ end of file------------excluding this line
and there are no spaces after any of the above words, each word is stored in one line.
I have an ifstream object that reads
char charVar;
int intVar;
inFile.read(reinterpret_cast(&charVar),sizeof(char));
cout <<"char value is : "<<charVar ;
inFile.read(reinterpret_cast(intVar),sizeof(int));
cout <<"int Var is : "<<intVar;
the first one works fine, ..but I cant seem to read the integer I m getting some negative numberss
and I have to use the ios::binay method to open the file,
-
March 29th, 2004, 05:04 PM
#2
What's with all the casting? I believe this should do what you want:
Code:
char charVar;
int intVar;
inFile >> charVar;
inFile.ignore(); // Ignore the newline character!
cout << "char value is : " << charVar ;
inFile >> intVar;
inFile.ignore(); // Ignore the newline character!
cout << "int Var is : " << intVar;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|