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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading char and int from data file????? 1

Status
Not open for further replies.

qb828

Programmer
Sep 27, 2000
98
US
hello! thanks millions in advance for your help..

i have to read in two columns of data file....
datafile look like this:
F 2
G 3
K -3
? 8
- 1
W -2
6
M 3
$ -1
first item is character(letters, white space, ?@$%^&, etc)
there are one or more white space between char and integer...
integer can be either positive or negative...

i used ifstream : ifstream infle >> buffer
and check to see i read in order so
cout << buffer ;
and i was able to read everything but white space!!!!!
it skiped the white space and messed up the char and int ordering....char int char int int char int...i need that whitespace..

i also used getline(buffer, 100) and this doesn't work either....
get() is good choice but there are unknown numbers of white space between char and int....
i can' t think of any other way....

please help ...

i am making doubly circular linked list
i have to insert char and int into the node in order it read from the file....int is a way of moving in the list positive means move forward that number of position: 3 mean advance forward 3 nodes...negative means retreat backward ...

thanks a lot again..

Q
 
char input[12];
ifstream inFile(&quot;input.txt&quot;);

inFile>>input;
cout<<&quot;character: &quot;<<input;
infile>>input;
cout<<&quot;number: &quot;<<input;

loop this and your done

Matt
 
oops... didnt realize you were using whitespace as well...

assuming the first character on a line is the character you want use
char ch;

inFile.get(ch);
inFile>>input;

loop that, ch will be the character and input will contain the value.

If you need to convert input to a value use atoi or sprintf

Matt
 
thanks a lot matt....
it worked........but my code for checking eof() causing me to read one more time..

while (!Infile.eof()){
Infile.ignore(); //without this it read funny
Infile.get(ch);
cout << &quot;CHAR : &quot; << ch;

Infile >> buffer;
cout << &quot; NUM : &quot; << buffer << endl;
}
datafile:
F 1
R -2
3
K 5
after reading:
CHR :F NUM: 1
CHR :R NUM: -2
CHR : NUM: 3
CHR :K NUM: 5
CHR :K NUM: <<=====here is extra line i don't need!!!

thanks millions again

Q
 
try

while(!InFile.eof() && InFile.peek()!= EOF)

// works for me :)

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top