Sonya Blade
Sonya Blade

Reputation: 399

Read space separated float values of matrix from text file in C

What can be wrong with my code below that it never opens the file. I've also tried with absolute file path but that doesn't helped me, I know physically that the file is there.

FILE *myfile;


    myfile= fopen("IN.txt",r);
    if (myfile != NULL)
    {
        while ( fscanf(myfile,"%lf",&test) !=eof )
        {
            printf("%f",test);
            printf("\n");
        }
    }
    fclose(myfile);

Upvotes: 0

Views: 2861

Answers (3)

Acsor
Acsor

Reputation: 1061

Maybe you want to do like this:

myfile= fopen("IN.txt","r");

This is because the second argument is of const char* type

And here:

while ( fscanf(myfile,"%lf",&test) !=EOF )

(C is case sensitive).

EDIT: And I'd like to suggest to use something like:

while ( (fscanf(myfile, "%lf", &test)) > 0){...}

Upvotes: 3

hazzelnuttie
hazzelnuttie

Reputation: 1481

Try printing the error using

printf ("Error opening file : %s\n",strerror(errno));

Upvotes: 1

Peter Miehle
Peter Miehle

Reputation: 6070

myfile= fopen("IN.txt",r);

schould be

myfile = fopen("IN.txt","r");

and make sure your filesystem is as case(in)sensitive as your filename suggests (so "IN.txt" is on UN*X different file to "in.txt")

Upvotes: 0

Related Questions