tags:

views:

1016

answers:

3

I'm writing a program and am having trouble using the scanf and fopen working together.

From what I can tell my erroneous lines seems to be:

FiLE * DataFile

DataFile = fopen("StcWx.txt","r");

scanf(DataFile, "%i %i %i %.2f %i %i", &Year, &Month, &Day, &Precip, &High, &Low);

The file it opens from has a list of weather data that looks like this:

1944 4 12 0 58 24

1944 4 13 0.4 58 29

1944 4 14 0.54 42 29

1944 4 15 0 43 27

(Those spaces are tabs)

The error that is displayed is "[Warning] passing arg 1 of `scanf' from incompatible pointer type"

Can anyone help me?

+15  A: 

I think you want fscanf not scanf.

chakrit
+1  A: 

You're using the wrong function. You should be using fscanf.

Derek Park
+2  A: 

Your code looks like it should be using fscanf, not scanf.

I would strongly suggest using fgets and sscanf rather than directly calling fscanf.

The fscanf can fail in ways that leave in doubt where your file pointer is. Using fgets to get whole lines and sscanf to scan the strings means you always know the state of the file pointer and it's very easy to back up to the start of the line (the string is still in memory).

paxdiablo
...and atoi() for the 4th parameter
Andrew Edgecombe