Find the answer to your Linux question:
Results 1 to 2 of 2
i have the following code for printing all the first word from a file......i need to print the word from the line number given by a user...........for example if the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2004
    Posts
    5

    WORDS IN A LINE


    i have the following code for printing all the first word from a file......i need to print the word from the line number given by a user...........for example if the line number is 3 i need to print the first word of third line.....how can i do it

    in = fopen("/root/mail/inbox/info.txt", "r");
    while(fgets(tmp,sizeof(tmp), in)!=NULL)
    while(fgets(tmp,sizeof(tmp), in)!=NULL)
    {
    for( i=0 ; tmp[i]!=' ' ; i++ )
    printf("%c",tmp[i]);
    }

  2. #2
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Just set a counter to count up from 1 when you read the first line of the file. When it gets to three, print word from that the line then exit your loop.
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

Posting Permissions

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