Thread: Reading integers from files..

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    Reading integers from files..

    Hi friends

    I am trying to read a list of double float from a txt-file into my c program, i.e. into an double array I have defined in my program.

    I have tried using fread( ), but it seems to be able to handle chars only. Is there a way to read integers?

    I am happy to join this forum, hope someone can give me a piece of advice.
    Last edited by pmk123; 10-04-2006 at 11:08 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If the "integers" are in a text file, chances are that it is text

    Like you see for example "12345678", you see 8 characters quite easily using any text editor.
    The same integer in a binary file would look like a mess with a text editor, and might read 4 bytes as "00 BC 61 4E" using a hex editor.

    Use fgets() to read each line, then sscanf() to extract an integer from that line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    thanx salem..but how to use sscanf()?
    can u explain..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    scanf("%d",&myint);
    Reads characters from stdin

    sscanf( buffer, "%d", &myint );
    Reads characters from buffer

    The advantage being that if the input is bad in any way, you can simply throw the line away by calling fgets() again. Cleaning up stdin is quite messy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    thanx a lot salem..

    should i also create a buffer now...

    and one more thing...
    in my data.txt file i have 36 integers in a line...
    i have to read each integer and assign a variable to it...i.e., how to extract each integer..?

    can u please help me...
    Last edited by pmk123; 10-04-2006 at 06:37 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
          if (sscanf(buf,"%d", &num) != 1)
          {
             printf("Error reading number.\n");
          }
          printf("%d\n", num);
    Now put that in a for-loop and change num to be an array and read into &num[i].

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > in my data.txt file i have 36 integers in a line...
    strtol() returns an integer conversion, and also a pointer to where it got to in the buffer.
    You can use this to work your way along the string.

    You can achieve the same effect using sscanf with the %n conversion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    thanx salem..

    but can u explain me with its implementation...

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    @swoopy

    theres an error in ur code..
    cannot convert int * to const char *

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >cannot convert int * to const char *
    As Salem said, you call fgets to read in a line, then call sscanf. buf should be the char array returned from fgets:
    Code:
    char buf[500];

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    @swoopy


    then how can i convert the characters in my buffer to the numeric integer array(num[i])...
    pls help me out

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    please help me out..
    as i said i have a data.txt file which contains 36 double floats in a line....
    i have to read them from the file and assign a variable name for each of the double float number...


    please..do help me out..

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >then how can i convert the characters in my buffer to the numeric integer array(num[i])...
    Quoting what I said earlier:
    Now put that in a for-loop and change num to be an array and read into &num[i].

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >as i said i have a data.txt file which contains 36 double floats in a line....
    The titile of this thread is: Reading integers from files..

    To read doubles, change the format specifier given to sscanf to %lf

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    this is what i have done...

    my datafull.txt contains;
    1.11111 2.111111 3.11111111 4.1111111111 5.1111111 6.1111111 7.1111111111 8.1111111111 9.11111111 10.11111111111 11.1111111 12.11111111 13.111111 14.1111111 16.11111111 17.1111111111 18.111111111 19.111111111 20.111111111 21.1111111 22.1111111 23.111111 24.1111111 25.1111111 26.1111111111 27.11111111 28.111111111111 29.11111111 30. 1111111 31.1111111 32.111111111 33.1111111111 34.11111111 35.111111111 36.11111111(end of first line)
    1.2222222 2.22222 3.22222 4.2222 .22222 6.22222 7.222222 8.22222 9.2222 10.22222 11.222222 12.2222222 13.22222 14.2222222 15.222222 16.22222 17.222222222 18.2222222222 19.222222222 20.22222222222 21.22222 22.22222 23.22222 24.22222 25.2222 26.2222 27.22222 28.2222 29.222 30.2222 31.22222 32.2222 33.22222222 34.222222 35.22222 36.222222222(end of second line)
    1.3333333 2.33333 3.33333 4.33333 5.3333 6.333 7.3333 8.3333 9.333 10.333333 11.33333 12.3333 13.3333 14.3333 15.3333 16.333 17.3333 18.333 19.333 20.333 21.333 22.3333 23.333 24.3333 25.333 26.3333333 27.33333 28.33333333 29.33333 30.3333 31.333 32.33333 33.3333 34.3333 35.3333 36.333333)end of third line)
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main(void)
    {
    FILE *fp;
    int i;
    double num[100];
    char buf[100];
    
    clrscr();
    fp=fopen("datafull.txt","r");
    
    fseek(fp,0,SEEK_SET);
    for(i=0;i<36;i++)
    {
    if(sscanf(buf,"%lf",&num[i])!=1)
    
    {printf("Error");  }
    printf("%lf\n" ,num[i]);}
    
    fclose(fp);
    getch();
    return 0;
    }
    Last edited by Salem; 10-05-2006 at 05:26 AM. Reason: Added code tags - learn to use them

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  2. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  3. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM