C++ Question on ofstream

Discussion in 'OT Technology' started by SPACECATAZ, Apr 4, 2009.

  1. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    Is it impossible to use the ofstream to append to an already written file? I'm trying to read an array and look for items in the array, once it finds something. It prints it out to a text file. It only works when I try to write it out to the input file and not the encoded file. So if I had '01010' at a spot in the array and my input.txt file has 'abcd' in it already. It will just overwrite whatever is in the file to '01010' and erase everything else. What I need it to do is if I have an encoded file already with binary numbers such as 0001111, I want it to add the '01010' at the end of that and not delete the text that's already there.

    Is that even possible?

    Code:
    /****************************************************************
     *                   writeCode                                  *
     ****************************************************************
     * writeCode() scans huffStore[x] for occurrences of codes. If  *
     *  an occurrence is found, the code is written out to file     *
     *  'outfile'.                                                  *
     ****************************************************************/
    
    void writeCode(char** huffStore, char* outfile)
    {
        ofstream out(outfile);
        for(int i; i < 256; i++)
        {
            if(huffStore[i] != '\0')
            {
               out << huffStore[i];
            }
        }
        out.close();   
    }
    
    I'm dealing with trying to encode a huffman tree btw. :noes:


    outfile = encoded file with binary numbers already in it from the huffman tree.
     
  2. vipergts24

    vipergts24 New Member

    Joined:
    Mar 10, 2005
    Messages:
    223
    Likes Received:
    0
    Location:
    Zomgland
    ofstream out;
    out.open("outfile.txt");
    out << data;
    out.close();

    so yes, just make sure you open the file and you will be able to write to it. If the file is opened elsewhere you can just pass the address of the ostream variable into your function and access it from there.
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
  4. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
  5. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    yes, that worked! thanks :bowdown::bowdown::bowdown:
    :bowdown::bowdown:

    Damn, I was struggling for a few hours trying to figure this out last night. Who knew it was as simple as changing two lines?

    Shit. :rofl:

    Code:
    void writeCode(char** huffStore, char* outfile)
    {
        ofstream out;
        out.open(outfile, ios::app);
        for(int i; i < 256; i++)
        {
            if(huffStore[i] != '\0')
            {
               out << huffStore[i];
            }
        }
        out.close();   
    }
    
     
  6. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    or you can use g and a spot functions to go to certain spots in the file
     
  7. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    I'm not sure what you mean, example?
     
  8. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    wasnt really relevant, ios::app is what you want
     
  9. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    My bad I ment seekg and seekp. You cna use this for moving the file cursor to append data to the end of a file in the middle or at the begining
     

Share This Page