Thread: Advanced File IO

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    42

    Advanced File IO

    Hello. I'm having trouble with file io. Hopefully someone can help.

    I don't know if anyone here knows much about huffman or arithmetic compression, but basically, you generate a sequence of bits and ship out groups of eight (bytes) to a file.
    My alphabet is extended ascii. I guess I'm supposed to be able to read and write any and all these characters. But I'm having problems:

    For example, in one program, the encoder was supposed to send the number of encoded characters as a 32 bit number to the output file. So I have a file with 26 characters. I convert this to binary and send out the bytes, least significant byte first:

    Binary: 00011010 00000000 00000000 00000000

    Ascii Character: Substitution NULL NULL NULL

    HEX: 1A 00 00 00


    I can send this out to a file just fine, but I CAN'T read all the characters (at the decoder).
    I'm using the fstream library (using ifstream, ofstream), and when I try to read in some of these "non-printable" characters, my stream fails and I can't do squat. It doesn't matter if i try reading it in to an integer variable or a char. I have to be able to read all the bytes.

    Is there some way around this?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Are you opening the file in binary mode? Should be something like:

    Code:
    int size;
    ifstream in( "data.enc", ios::binary );
    in.read( ( char* )&size, sizeof( int ) );
    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;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by Link_26 View Post
    ... my stream fails and I can't do squat ...
    That's the technical description of your problem?

    Sorry, I couldn't resist. But all kidding aside, people would be better able to assist if you would be more specific about what's going wrong, and post at least enough of your code to show exactly how you are attempting to do the i/o. "send out the bytes" and "read in" are not explicit enough, since there are various ways to do those things.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    lol

    Anyway, ok, here is some of my code.

    My encoder outputting the file size as 4 bytes:

    Code:
       /////////////get file size and send to decoder///////////////////
        unsigned long count = 0;
        INPUT_FILE.get();
        while(!INPUT_FILE.eof())
        {
            count++;       //use tellg instead?
            INPUT_FILE.get();
        }
        INPUT_FILE.clear();
        INPUT_FILE.seekg(0);
        
        unsigned long temp = 255;
        for(int i = 0; i < 4; i++)
        {
            unsigned long x = (count & temp);
            x = x/int(pow(float(256), float(i)));
            OUTPUT_FILE << char(x);
            temp = temp * 256;
        }
    
    //count (32 bits long) is divided up into four bytes and sent out
    yeah its kind of ugly but it works. My problem supposedly originates when my decoder tries to read these four bytes:

    Code:
        ifstream INPUT_FILE;
        ofstream OUTPUT_FILE;
        
        openFiles(INPUT_FILE, OUTPUT_FILE);
        
        /////////////////////get file size/////////////////////
        unsigned long count = 0;
        unsigned long temp = 256;
              
        for(int i = 0; i < 4; i++)
        {
            int t;    
            t = INPUT_FILE.get(); //doesn't work either if i use a char and get(char)
            if(t < 0)
                t+=256;
            unsigned long x = t;
            x = x*int(pow(float(temp), float(i)));   
            count += x;
        }
    In some cases this won't work. The encoder put my file size (26) into byte form, which is the substitution ascii character. ifstream refuses to read this character, but I've got to have a way to read all 256 ascii characters.


    Thanks Sebastiani. I'll try it.
    Last edited by Link_26; 03-23-2009 at 09:28 PM.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    ifstream is really not meant for binary stuff, in my opinion. I feel much more at home with FILE * usually, or streambufs. That said:

    1) Your measurement of the file size. seekg() to the end, and tellg(), instead of reading the whole file off of disk. That'll be a whole heck of a lot cheaper than reading the entire file and discarding it. Better, if you can (and it's not always possible), don't care about the file size. Read the file straight through, beginning to end. The benefit here is you can then read non-seekable files, such as pipes.

    2) Try read(), although I'm not sure why get() doesn't work (but my gut says I ran into this years ago, I just forget what the issue was!) Since you know you're going to read 4 bytes, request 4 bytes, not 1,1,1,1.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Use read() and write(), not get() and definitely not << for binary data.

    Also, check out this thread, where I was asking about pretty much the same kind of i/o issue:

    http://cboard.cprogramming.com/c-pro...esnt-work.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM