Find the answer to your Linux question:
Results 1 to 2 of 2
hi, i have a file like below Code: 12 23.4 56.7 14 10 23 -20 9.3 2.4 -2 2.1 4.5 ... .... ..... something like this. i want a user ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2003
    Location
    x1234
    Posts
    4

    file handling in C


    hi, i have a file like below
    Code:
    12         23.4        56.7
    
    14          10         23
    
    -20         9.3        2.4
    
    -2          2.1        4.5
    
    ...    ....       .....
    something like this.

    i want a user will input a number ... suppose user is inputting -20

    then my program will search the first column...if it finds -20 then the value 9.3 and 2.4 will be picked up.

    if it do not find that number then nothing will be returned.

    how can i do it?

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I'd suggest something like this:
    Code:
    char buf[1024];
    
    int main(int argc, char **argv)
    {
        double num[3], match;
        
        match = atof(argv[1]);
        while(!feof(stdin))
        {
            gets(buf);
            sscanf(buf, "%f %f %f", num, num + 1, num + 2);
            if(num == match)
            {
                printf("%f %f\n", num[1], num[2]);
                break;
            }
        }
        return(0);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •