Thread: using fgets()

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    using fgets()

    so i was wondering, for this function how would you be able to read a certain line from a text file that has multiple lines because from what i read about its definition it does not seem to be possible.

    text file looks like this:
    (
    monkeys
    dogs
    cats
    birds
    )

    and i want the function to read only cats. can i get some help thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    read lines till you get to the required line using loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    here is what i have

    Code:
    main()
    {
           FILE *inp;
            inp= fopen("clues.txt", "r");
          int row, i;
          char clue[46];
          row = Rand_NUM(0,45);//Rand_NUM is used to generate a random number
    
           for (i=0; i<=row;++i);
    {
            char *fgets(char *clue, int 30, FILE *inp);//problem with this line
    }
    return(clue);
    printf("%s", clue);
    [/PHP]


    i keep getting an error with the fgets and it prints some kind of alien language that looks nothing like english. whats worng?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This line
    Code:
    char *fgets(char *clue, int 30, FILE *inp);//problem with this line
    looks like some kind of alien language that looks nothing like C.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385 shows how you might use fgets.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Code:
    fgets(buf, sizeof(buf), stdin)

    what does stdin stand for this came from the example

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM