UNABLE TO READ DATA FROM A FILE

I want my program to read a data file (i.e myData.dat) that contains strings, integers and character. I'm problem is that i don't know how to read these different data types and output them in a different file. I don't know if i need to use three different while loops, each reading its own data type. please tell how to solve this one. thanking you in advance.
If the order of strings, integers and chars is known and fixed:

1
2
3
4
5
6
while(thereIsStillDataToRead)
{
  inFile >> someString;
  inFile >> someInteger;
  inFile >> someChar;
}

(with the inputs in the appropriate order) will be fine.
Last edited on
the following is the contents of the file i want my program to read.
Peter 3 / + - *
Dahne 8 + - / *
Renee 7 - * + /
Charl 1 * / + -
Johan 2 / - * +
Sipho 4 * - / +

I think you gave a good idea but i have a problem with the part where you read characters. Its seems like in one iteration your loop will read one string (i.e name), one integer, and one operator and then it jumbs to the next iteration. I want it to read all characters(i.e operators) before it moves on to the next line.
Peter 3 / + - *

This is a string, an int, a char, a char, a char, a char.

1
2
3
4
5
6
7
8
9
while(thereIsStillDataToRead)
{
  inFile >> someString;
  inFile >> someInteger;
  inFile >> someChar0;
  inFile >> someChar1;
  inFile >> someChar2;
  inFile >> someChar3;
}
Last edited on
i'm gonna try using your method and i will let you know of the outcome soon. thanks a million.
hi Moschops, your loop work perfectly fine but it reads Sipho 4 * - / + twice. is it because i used while(!infile.eof()) as my loop's condition. how can i solve this?
Open your file in notepad(or whatever text editor you use) and see where the cursor is when it's at the end of the file.

When I read in my vertex files, if the last line is a blank line, I get erroneous data for the last read.
Last edited on
the cursor is at the begiunning of the file in my input file. so i don't think that the cursor is the problem. As far as i know the eof() function will allow the loop to perfom one more iteration after the loop has reached the end of file. I just wanna know how this can be avoided.

the cursor is at the begiunning of the file in my input file. so i don't think that the cursor is the problem.

The cursor is not the issue. It's just an indicator.

Ok, let me rephrase...

Do you have a blank line at the end of your file? A blank line is still not an eof. It's not an eof until it actually cannot read anymore. A blank line is any line w/o text after your text, even if the only space is the space the cursor sits.

Otherwise, post your code.
Last edited on
Topic archived. No new replies allowed.