Read from file negative numbers C++

2.8k Views Asked by At

I have to get numbers from file and some of them are negative. So, how can I do that? (C++)

Now I'm trying just this (without something special):

U1.txt file:

4 5 -1 6 -2

My code:

ifstream fd(FD);
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;
1

There are 1 best solutions below

0
On

Your code is basically correct, except for that typo with n4 missing. This:

ifstream fd("U1.txt");
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;

Will do what you expect, modulo error conditions.