Reading a floating point from a .txt file into an array

This is a discussion on Reading a floating point from a .txt file into an array within the C Programming forums, part of the General Programming Boards category; Hello all.. I'm fairly new to programming and am trying to get the following implemented. I have a .txt file ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Reading a floating point from a .txt file into an array

    Hello all..
    I'm fairly new to programming and am trying to get the following implemented. I have a .txt file which contains something like this...
    1.34324 10202043
    1.78458 19430437
    1.93287 10545348
    2.37432 21025745
    ...

    etc..

    Now initially what i need to do is, to read the first floating point number into a variable in this case 1.34324. Then I need to have an array of float that contains all the floats from the first column of the file.

    Here is the code I have written but as you can see once i fscanf the first float into the variable t0, the array a[i] starts scanning from the second line and not the first as I would like it to. How can I make the file pointer go back to beginning of file.
    Code:
    #include<stdio.h>
    int main()
    {
      FILE *fptr;
      int i=0;
      float t0;
      int bin0;
      float a[11];
      int b[11];
      fptr = fopen("flt_read.txt","r");
      if(fptr == NULL){
        printf("Error!! Cannot open file \n" );
        return 1;}
      else
        {
          printf("File opened successfully :) \n");
          fscanf(fptr,"%f %d",&t0,&bin0);
          printf("t0 = %f\n",t0);
          while(!feof(fptr))
        {    
          fscanf(fptr,"%f %d",&a[i],&b[i]);
          printf("%f\n",a[i]);      
          ++i;
        }
        }
    
      return 0;
    }
    I have 2 other problems
    1) In the output I see a negative floating point value that is different every time i execute the file. Almost like some error value. I don't know what this is..

    2) Is there anyway to read just the first column without having to read the second one into an integer array. If i just say
    Code:
    fscanf(fptr"%f",&a[i])
    it reads the entire line as a float and I don't want this..

    Pls help.. Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by astrogal View Post
    Hello all..
    How can I make the file pointer go back to beginning of file.
    You can use rewind(fptr) to put the file pointer back at the beginning. Just put it just before your while(!feof(fptr)) statement.

    Quote Originally Posted by astrogal View Post
    I have 2 other problems
    1) In the output I see a negative floating point value that is different every time i execute the file. Almost like some error value. I don't know what this is..

    2) Is there anyway to read just the first column without having to read the second one into an integer array. If i just say
    Code:
    fscanf(fptr"%f",&a[i])
    it reads the entire line as a float and I don't want this..

    Pls help.. Thanks in advance
    1) I tried your code with the example values you provided and I don't see any negative values. What does your output look like exactly?

    2) Can't you just read both columns like you are doing now and ignore the second one? Otherwise you have to write some function that just reads until the next newline character and dicards the data, something like:
    Code:
    while(!feof(fptr))
    {    
        fscanf(fptr,"%f",&a[i]);
        while ((fgetc(fptr) != '\n') && (!feof(fptr))) { /* do nothing */ }
        printf("%f\n",a[i]);      
        ++i;
    }
    Last edited by iceaway; 09-28-2011 at 12:34 AM.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I wouldn't even use rewind. I'd just do something like
    Code:
    a[0]=t0;
    b[0]=bin0;
    i=1;
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by iceaway View Post
    You can use rewind(fptr) to put the file pointer back at the beginning. Just put it just before your while(!feof(fptr)) statement.



    1) I tried your code with the example values you provided and I don't see any negative values. What does your output look like exactly?

    2) Can't you just read both columns like you are doing now and ignore the second one? Otherwise you have to write some function that just reads until the next newline character and dicards the data, something like:
    Code:
    while(!feof(fptr))
    {    
        fscanf(fptr,"%f",&a[i]);
        while ((fgetc(fptr) != '\n') && (!feof(fptr))) { /* do nothing */ }
        printf("%f\n",a[i]);      
        ++i;
    }
    Read Why it is bad to use feof() to control a loop.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by AndrewHunter View Post
    Thank you, I had never heard about that before.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by iceaway View Post
    You can use rewind(fptr) to put the file pointer back at the beginning. Just put it just before your while(!feof(fptr)) statement.
    Thanks.. does rewind(fptr) always make the pointer point to beginning of file or just decrement it and make it point to previous line in the file?


    Quote Originally Posted by iceaway View Post
    1) I tried your code with the example values you provided and I don't see any negative values. What does your output look like exactly?
    This is the output i see... Note the values in my .txt file are different than in my original post..
    File opened successfully
    t0 = 1.450014
    1.501242
    1.602542
    1.965250
    1.972525
    2.522452
    2.550225
    2.825050
    2.992050
    3.052587
    3.689564
    -0.000014


    2) Can't you just read both columns like you are doing now and ignore the second one? Otherwise you have to write some function that just reads until the next newline character and dicards the data, something like:
    Quote Originally Posted by iceaway View Post
    Code:
    while(!feof(fptr))
    {    
        fscanf(fptr,"%f",&a[i]);
        while ((fgetc(fptr) != '\n') && (!feof(fptr))) { /* do nothing */ }
        printf("%f\n",a[i]);      
        ++i;
    }
    Thanks..Will keep this in mind. I am just trying to reduce the memory i take up with the code..

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by AndrewHunter View Post
    Thanks.. I didnt know..

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by astrogal View Post
    Thanks.. does rewind(fptr) always make the pointer point to beginning of file or just decrement it and make it point to previous line in the file?
    It always returns to the beginning of the file.
    Quote Originally Posted by astrogal View Post
    This is the output i see... Note the values in my .txt file are different than in my original post..
    File opened successfully
    t0 = 1.450014
    1.501242
    1.602542
    1.965250
    1.972525
    2.522452
    2.550225
    2.825050
    2.992050
    3.052587
    3.689564
    -0.000014
    Try changing your while loop to not use !feof(ftpr) as andrew suggested, instead use
    Code:
    while(fscanf(fptr,"%f %d",&a[i], &b[i]) == 2)
    {            
        printf("%f %d\n", a[i], b[i]);      
        ++i;
    }
    if that doesn't work, please post your original file with values (as an attachment).

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0; x < SIZE && fgets( buf, BUFSIZ, fp ); x++ )
    {
        if( sscanf( buf, "%f", &array[ x ] ) != 1 )
            printf( "Your stuff broke.\'%s\'\n", buf );
    }
    Something like that should work.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by quzah View Post
    Something like that should work.

    Quzah.
    Neat rainbowish color-formatting

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks all.. The code is working fine now.. Will get back if I have any other queries

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Code:
    for( x = 0; x < SIZE && fgets( buf, BUFSIZ, fp ); x++ )
    {
        if( sscanf( buf, "%f", &array[ x ] ) != 1 )
            printf( "Your stuff broke.\'%s\'\n", buf );
    }
    Something like that should work.

    Quzah.
    Yeah... *IF WE COULD READ IT!*

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Yeah... *IF WE COULD READ IT!*
    You have to love quzah's dry humor about the color selection scheme. (PSST)I think he is mad because it doesn't match his tutu
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Yeah... *IF WE COULD READ IT!*
    Oh you're one of *those people*.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading the number of floating point numbers in an array
    By mfurseman in forum C Programming
    Replies: 6
    Last Post: 03-16-2011, 11:55 AM
  2. floating point array
    By luigi40 in forum C# Programming
    Replies: 2
    Last Post: 12-09-2005, 03:37 PM
  3. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  4. Reading 64 bit IEEE floating point from a file
    By rcobb in forum C Programming
    Replies: 1
    Last Post: 04-23-2003, 07:28 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21