reading file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • butterfly21
    New Member
    • Oct 2006
    • 7

    reading file

    Hi i have a very simple code to read values from a file. However It isnt taking in all the values correctly.

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int i[100000000];
    int t,r;
      
    main()
    {
      FILE *dat;
      dat = fopen("ordered.dat","r");
    
    r=1;
      for(r=1;r<50;r++)
      {
        fscanf(dat,"%d",&i[r]);
        printf("%d\n",i[r]);
    
      }
      
    }
    Here are a list of the values i am using to read:

    56
    1516
    211516
    31121516
    4112131516
    511213141516
    611213142516
    512213141526
    413213142516
    412223241516
    314213241516
    412223241516
    314213241516
    412223241516
    314213241516
    412223241516

    Would really appreciate any help you can give me, on why i am not getting these values when i read the file!
    Thanks
  • kirubanantham
    New Member
    • Nov 2006
    • 8

    #2
    Hai ,
    You just try this code,
    #include <math.h>
    #include <stdio.h>

    double i[100];
    int t,r;

    main()
    {
    FILE *dat;
    dat = fopen("ordered. dat","r+");

    r=0;
    for(r=0;r<50;r+ +)
    {
    fscanf(dat,"%d" ,&i[r]);
    printf("%d\n",i[r]);
    }
    fclose( dat );
    }
    You are declared variable i as an integer first but when you are reading you are using type specifier as a double.

    Cheers,
    Kiruba.K

    Comment

    • svsandeep
      New Member
      • Nov 2006
      • 15

      #3
      Originally posted by butterfly21
      Hi i have a very simple code to read values from a file. However It isnt taking in all the values correctly.

      Code:
      #include <math.h>
      #include <stdio.h>
      
      int i[100000000];
      int t,r;
        
      main()
      {
        FILE *dat;
        dat = fopen("ordered.dat","r");
      
      r=1;
        for(r=1;r<50;r++)
        {
          fscanf(dat,"%d",&i[r]);
          printf("%d\n",i[r]);
      
        }
        
      }
      Here are a list of the values i am using to read:

      56
      1516
      211516
      31121516
      4112131516
      511213141516
      611213142516
      512213141526
      413213142516
      412223241516
      314213241516
      412223241516
      314213241516
      412223241516
      314213241516
      412223241516

      Would really appreciate any help you can give me, on why i am not getting these values when i read the file!
      Thanks

      Dude,

      For an integer, typical size is 4 bytes which means it can store as much as 4,294,967,296./* calculated as 2 power (8*4) */ This doesn't mean you can store from 0 to 4,294,967,296, instead you can store from -2147483648 to +2147483647.

      Now if you make a attempt to store 2147483648 in your program it will take as
      -2147483648 and for 2147483648 --> -2147483647 and so on..

      I hope this answered your question.

      Regards,
      Shaggy@FTF

      Comment

      Working...