using fgets()

This is a discussion on using fgets() within the C Programming forums, part of the General Programming Boards category; so i was wondering, for this function how would you be able to read a certain line from a text ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    read lines till you get to the required line using loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 05: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, 02: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

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