Unwanted output from file

Hi,

I am taking 101 data from a file and directly printing it on screen and expecting output like this


num=1  name=name1  type=type1
num=2  name=name2  type=type2
.
.
.
.
num=101 name=name101  type=type01


but instead I am getting this


num=1  name=name1  type=type1
num=2  name=name2  type=type2
.
.
.
.
num=101 name=name101  type=type01
num=0 name= type=


What could be the reason?

here's my class

1
2
3
4
5
6
7
8
class qwer{
public:
int num;
char name[21];
char type[15];

};
qwer q[100];


and code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ifstream fin;
int i=0;
fin.open("dex.txt",ios::in);
while(!fin.eof())
{
if(fin.eof())
 break;
else
{i++;

 fin>>q[i].num;
 cout<<"\n NUM="<<q[i].num;
 fin>>q[i].name;
 cout<<"\t Name="<<q[i].name;
 fin>>q[i].type;
 cout<<"\tType="<<q[i].type;
 }
 }

 fin.close();


Please help
Last edited on
What could be the reason?
1
2
3
4
while(!fin.eof())
{
if(fin.eof())
 break;
This.

1
2
if(fin.eof())
 break;
Is especially useless, as break will never be executed (if condition is true, while loop will be terminated earlier).

Do not loop on eof. It leads to "one more iteration than needed" errors.

Proper way to read your file:
1
2
3
4
5
6
7
8
std::ifstream fin("dex.txt"); //Use RAII here, ios::in is implied on input streams
int i = 0;
while(fin >> q[i].num >> q[i].name >> q[i].type) {
    std::cout << "\n NUM="  << q[i].num  <<
              << "\t Name=" << q[i].name <<
              << "\t Type=" << q[i].type;
    ++i;
}
or
1
2
3
4
5
6
std::ifstream fin("dex.txt");
for(int i = 0; fin >> q[i].num >> q[i].name >> q[i].type; ++i) {
    std::cout << "\n NUM="  << q[i].num  <<
              << "\t Name=" << q[i].name <<
              << "\t Type=" << q[i].type;
}
Last edited on
Woah!

I didn't knew that there is something like fin >> q[i].num >> q[i].name >> q[i].type

It was awesome, thanks buddy

could you explain what while(fin >> q[i].num >> q[i].name >> q[i].type) does exactly?

By looking at it I think it checks the statement till the fin puts details inside q. Am I correct?
could you explain what while(fin >> q[i].num >> q[i].name >> q[i].type) does exactly?
It reads data from fin to those three variables in order. Then it checks result of operation. As operator>> returns stream it operates on it is equivalent to
1
2
while(  fin >> q[i].num >> q[i].name >> q[i].type , //comma operator
       fin ) { // std::ios_base::operator bool is called 

Opeartor bool returns true if stream in good state (no errors occured and stream is ready for next operation) and returns false if some operation failed (there were nothing to read in file)

So you can read that as "while read is succesfull, do stuff"
Oh thanks!

but can I use cin instead of fin?

also how can I use is for multiple fin's? (lets say fin1, fin2 - I cant use fin1>>a>>fin2b>>c for 2 fin's)
but can I use cin instead of fin?
Yes, you can. This technique works with any stream.

also how can I use is for multiple fin's?
Something like that?
1
2
3
while (fin1 >> a && fin2 >> b) {
    //do stuff
}
Last edited on
no semicolons? and if you use cin wont the loop be infinite?
Last edited on
no semicolons?
In condition expression there is no need for semicolons: it is one logical expression "if a is read from fin1 successfull and (&&) b is read from fin2 succesfully"

and if you use cin wont the loop be infinite?
You can pass invalid data to trigger loop termination or pass EOF signal manually (ctrl+Z on newline on Windows, ctrl+D on Linux)
Thanks.... How do you know so many cool stuff??
Topic archived. No new replies allowed.