Thread: simple fgets question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    simple fgets question

    Hiya, I was wondering if anyone could help me with a simple question on fgets().

    I'm reading in an unknown number of characters into a char pointer using fgets... Now the arguments for fgets are (arrayname, size of array, stdin)

    If I have a pointer instead of an array, how do I express the size of it since I don't know the size yet?

    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm reading in an unknown number of characters into a char pointer using fgets...
    Really, are you sure? You might want to consider playing around with a toy program that does what you want to do. The results may surprise you.

    >If I have a pointer instead of an array, how do I express the size of it since I don't know the size yet?
    sizeof p, but that's probably not what you want since a pointer is 4 bytes and your input is probably more than that. If you're reading an unknown number of characters then why use fgets at all since you have to know the size?

    Here were my results based on your description of the problem:
    Code:
    int main(void) 
    {   char *p = NULL;
    
        fgets( p, sizeof p, stdin );
        printf( "%s", p );
    
        return EXIT_SUCCESS;
    }
    Result: Big time assertion error in fgets()
    Code:
    int main(void) 
    {   char *p = malloc( 255 * sizeof(char) );
    
        fgets( p, sizeof *p, stdin );
        printf( "%s", p );
        free( p );
    
        return EXIT_SUCCESS;
    }
    Result: fgets didn't even accept input despite compiling fine.
    Code:
    int main(void) 
    {   char *p = malloc( 255 * sizeof(char) );
    
        fgets( p, sizeof p, stdin );
        printf( "%s", p );
        free( p );
    
        return EXIT_SUCCESS;
    }
    Result: Reads great, saves 3 characters and a nul. Care to guess why?
    Code:
    int main(void) 
    {   char *p = NULL,
             array[256];
    
        p = fgets( array, sizeof array, stdin );
        printf( "%s", p );
    
        return EXIT_SUCCESS;
    }
    Result: Works great, but requires a buffer
    Conclusion: You might want to reconsider your method of input and/or your method of saving that input.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    Originally posted by Prelude

    Conclusion: You might want to reconsider your method of input and/or your method of saving that input.

    -Prelude
    Well the method of saving the input cannot change. I am piping the input in from UNIX... And the input will be a large number of words, each seperated by '\n'.

    Is there another possible function I can use other than fgets? I have been adviced not to use gets.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have been adviced not to use gets.
    Rightly so. gets is notoriously unsafe, in this situation it works fine but you have to malloc your pointer and you're back at square one with a new potential bug.

    This sounds like a nightmare, you have my sympathy.

    Try using a buffer and a dynamic array. Read one word up to the newline, get it's length and increase your dynamic array by that much, concatenate the buffer to the end of it and then assign your pointer to the resized array. If you can offer more information as to what you plan on doing with the input once you have it, and the estimated size of the input that would help me in thinking of another solution.

    -Prelude
    Last edited by Prelude; 01-27-2002 at 06:35 PM.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    Originally posted by Prelude
    This sounds like a nightmare, you have my sympathy.

    Try using a buffer and a dynamic array. Read one word up to the newline, get it's length and increase your dynamic array by that much, concatenate the buffer to the end of it and then assign your pointer to the resized array. If you can offer more information as to what you plan on doing with the input once you have it, and the estimated size of the input that would help me in thinking of another solution.

    -Prelude
    I don't believe I know how to use a buffer, I haven't programmed in a year so I'm pretty rusty. Is a dynamic array just a pointer of some sort?

    Once I have the input (which is most likely a novel or something with a lot of words), I'm supposed to do some calculations to find statistics like the total number of words, average size of word, total number of distinct words, words that only appear once in the entire input, etc.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Are you recieving some sort of streaming data or are you reading from a file?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't believe I know how to use a buffer
    A buffer is just a middleman for your input. Usually it's a very large array that holds data temporarily while you create a place for it elsewhere and can then refill the buffer.

    >Is a dynamic array just a pointer of some sort?
    A dynamic array is a char pointer that is reallocated to increase or decrease its number of elements according to how much data is in it. You don't have to know the size of your input but you do have to know how much to resize the array, hence a buffer to read a single word so you can get a size.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    Originally posted by Sebastiani
    Are you recieving some sort of streaming data or are you reading from a file?
    I believe it's some sort of streaming data. I need to manipulate the original file using some unix utilities and without using any temp files, put the manipulated data into my C program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. fgets() question
    By caduardo21 in forum C Programming
    Replies: 9
    Last Post: 02-01-2005, 07:03 PM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM