Thread: fgets array size

  1. #1
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195

    fgets array size

    How big is too big?

    I wrote a program for school that does Huffman compression. I use fgets to get my encoded string for decoding. Right now the encoded file is stored as one continous string (no newline). So I have been having to use numbers like 100,000 in the fgets call just to decode average size files.

    fgets(array pointer, 100,000, infile);

    Is there a better way to do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you sure you're using the right routine?

    It seems to me that if the data is a Huffman compression stream, that it would be binary data.

    In which case, you should be using fread, not fgets

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    I actually am supposed to not use binary data for this. The TA told me it is easier for him to check that way. To get the actually compression ratio you have to divide by eight. This is another reason for the large sized array.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well there's no need to read the whole lot in one array, fgets will read in a loop like so.

    Code:
    char buff[1000];
    while ( fgets( buff, 1000, infile ) != NULL ) {
      // reads the file in 999 byte blocks
    }

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Thanks, that is exactly what I was asking. To make sure I understand, fgets will return to the next positon from where the filepointer stopped last time. Is this correct?

  6. #6
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    One more related ques, what is the max number for an array size? I guess it will vary from machine to machine. But what is a ballpark figure?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Your TA is a blockhead! Use fread and then you'll be able to even keep yer newlines in the 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;
    }

  8. #8
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    I won't disagree on that...

    I am actually considering doing that, but I am going to talk to my prof first. I am sure he won't care.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Sebastiani
    Your TA is a blockhead! Use fread and then you'll be able to even keep yer newlines in the file....
    I'm not too up on Huffman, but at a guess I'd say the encoded data could well contain non-printable characters, including those nasty 0x00's.

    If that's the case, when you use fgets (which is used to get a string, and automatically terminate it with a 0x00), won't you get confused between 0x00's in the data, and those added by fgets?

    If you use fread, that will read anything into the array, and return the number of items read. Data integrity is maintained.

    There, that's my thoughts anyways......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > One more related ques, what is the max number for an array size?
    Depends on your compiler / OS
    Real DOS is limited to 64K
    Pretty much everything else is measured in MBytes

    For reading text files, the BUFSIZ macro in stdio.h is a good bet

    > but at a guess I'd say the encoded data could well contain non-printable characters
    It was my impression that the binary data has been 'ascii-fied' - like uuencode or MIME, only different.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM