0

When I run the program, the numbers I'm getting are completely ridiculous such as -39389014 when the number in the text file is 20. Here is what my text file looks like:

20 20
40 30
80 40
90 20
60 10
18.0

And the Code:

#include <stdio.h>
#define SIZE 5

int main(void){
    FILE *in = fopen("pfile1.txt", "r");
    int x[5], y[5], i;
    double h;

    for (i=0;i<SIZE;++i){
        fscanf(in, "%d %d", &x[i], &y[i]);
    }

    for (i=0;i<SIZE;++i){
        printf("%4d %10d\n", x[i], y[i]);
    }


    fscanf(in, "%lf", &h);
    printf("%lf\n", h);
    fclose(in);

    return(0);
}
Michi
  • 5,175
  • 7
  • 33
  • 58

1 Answers1

0

Make sure that your "pfile1.txt" file is placed next to your project settings. to make sure about the place of your file should be, change the permission of open to "w+". This option will try to open that file or create it if not found. The replace that file with yours. Everything will be work without any problem :)

Just Hint on the fly: Don't make any operation on any pointer unless you make sure that it is not NULL. so, surround your code with to avoid any crashing

if( NULL != in)
{

    for (i=0;i<SIZE;++i){
        fscanf(in, "%d %d", &x[i], &y[i]);
    }

    for (i=0;i<SIZE;++i){
        printf("%4d %10d\n", x[i], y[i]);
    }


    fscanf(in, "%lf", &h);
    printf("%lf\n", h);
    fclose(in);
}
  • Ive put the file in the same folder as my project and changed the permission for fopen to "w+" but it's still not working – Dylan Posey Mar 23 '16 at 22:20
  • What I mean if you are not sure where to put your file. Remove all relevant files, then use "w+". It will create a file. Then replace that file with yours and try again but don't forget to return permission to "r" again before to avoid your data to be cleaned. – Awad A. Bekhet Mar 23 '16 at 22:24
  • Oh I understand, let me try – Dylan Posey Mar 23 '16 at 22:28