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

Read from file

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I have a file I'm opening. This info looks like this:

123 725
205 115

and so on. There is a space between the two numbers. When I use fscanf I get the first number, but I get a negative # for the second number. But if I try to store the space I still get a negative #. This is that part of the code:

status = fscanf(inp, "%d%d", &CustNumb, &KWHUsedForCust);

CustNumb is always right, but KWHUsedForCust is a negative #.

Can anyone help?

Thanks!
Erica
 
Hello,

Since fscanf works similar to scanf, your code should have a space between the two "%d", like this:

status = fscanf(inp, "%d %d", &CustNumb, &KWHUsedForCust);

Also, status should be checked to see that it is ok.

if (status == 2)
...do something

Im not sure if you have that in the code and didn't include it, but you could do something like this if it is reading in as a loop,

while ((fscanf(inp, "%d %d", &CustNumb, &KWHUsedForCust)) == 2)
...do stuff

Hope that helps.

-Tyler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top