1

So I've been having some difficulties storing some numbers from a .dat file. The file is as follows:

1
2
7
10
9
4
0
5
6
8
3

The code I have to try and retrieve this information is:

#include <stdio.h>
#include <stdlib.h>

#define N 11

int main(int argc, char** argv) {

    //Declare variables: number of elements, counter, array, & valuesto
    //print
    int i;
    double a[N], num, temp;
    FILE* dat;

    //Print space for program cleanliness
    printf("\n");

    //Open file
    dat = fopen("zero.dat", "r");      

    //Initialize i  
    i=0;

    //Read in infromation from file
    while(!feof(dat) && i < N){
        printf("This is loop number %d\n", i);
        num = fscanf(dat,"%lf", &temp);
        printf("Temp variable is stored as: %f\n", temp);
        printf("Number of characters read in: %d\n\n", num);
        a[i] = temp;
        i++;
    }        

    fclose(dat);    

    for(int j=0;j<i;j++){

        printf("%f\n", a[j]);

    }

    //Exit program
    exit(EXIT_SUCCESS);
}
/**************************************************************************/

And the output is:

This is loop number 0
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 1
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 2
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 3
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 4
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 5
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 6
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 7
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 8
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 9
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 10
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

Also, it should be noted that I am using NetBeans IDE 8.0.2; and the zero.dat file is located within the same project folder as the source.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    Your `num` is a double, but you print it as an integer. The return value of `fscanf` is an int, but it does not return the number of scanned characters, but the number of format conversions or the special value `EOF` to indicate that the end of the file has been reached. – M Oehm Dec 03 '15 at 08:19
  • 3
    always ever check the result of `fopen();` – Peter Miehle Dec 03 '15 at 08:19
  • Your example works for me, if I change `num` to be an `int`. You should check whether the file could be opened as Peter Miehle pointed out. Another thing that might cause conversion errors is the file encoding. For example if your file has a BOM (byte order mark), `fscan` tries o convert a double from it, but fails. Because failed conversions return the file pointer to its old position, all conversions fail. Try saving your file as plain ASCII or UTF-8 without BOM. – M Oehm Dec 03 '15 at 08:29
  • 1
    OT, but applies also: http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – alk Dec 03 '15 at 08:30
  • Another nitpick: Make `i` and `j` be `size_t`s or at least `unsigned`, as there is no need for negative values. – alk Dec 03 '15 at 08:33
  • @MOehm At this point, I think that the encoding of the file is the source of the problem; as I included the following lines of code: `if(dat == NULL){ printf("\nCould not open the file\n"); exit(EXIT_FAILURE); }` but it still runs the exact same because the file IS opened, I'm really just at a loss... – Trent Reynolds Dec 03 '15 at 08:42
  • 1
    You could do a hex dump of the file to see whether there are any "strange" characters at the beginning of the file. If so, change the encoding in your text editor and save the file again. – M Oehm Dec 03 '15 at 08:57

1 Answers1

1

your code prints Number of characters read in: 1541763072 because fscanf returns a int and you're collecting it in a double variable, not sure why junk values are put but change it to a int varaible something like this

int j;
j=fscanf(dat,"%lf", &temp);
printf("Number of characters read in: %d\n\n", j);

It is a good programming practice to check whether file exists or not. You just get a segmentation fault if file is not present, and you won't know why it returns a segmentation fault. instead handle it

dat = fopen("zero.dat", "r");
if(dat == NULL)
{
     //your message
     return;
}
sameera sy
  • 1,708
  • 13
  • 19
  • I added this conditional formatting, and it still runs the same, suggesting that the file IS opened; however, it now prints zeros instead of random junk. So it seems that the `fscanf` isn't reading in properly, but I don't know why... – Trent Reynolds Dec 03 '15 at 09:06
  • try doing a `fgets` and see whether the data is at least accessible – sameera sy Dec 03 '15 at 09:17