Thread: write an array of unspecified size to a file

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    16

    write an array of unspecified size to a file

    how do i loop until this is done?
    the way it is written currently, it only writes the first 512 characters. how can i make it loop until it writes the whole file? i think i need some kind of loop in my writer code section where i have set up the while loop that i dont know how to fill in. any ideas?




    //reader
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            cout << "in reader" << endl;
            char readbuffer[512];
            int ipipe = atoi(argv[2]);
            ifstream ifs(argv[1]);
    
            while (!ifs.eof())
            {
                    ifs.read(readbuffer, sizeof(readbuffer));
                    write(ipipe, readbuffer, ifs.gcount());
            }
    
            close(ipipe);
    
    
            return 0;
    }

    //writer
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            cout << "in writer" << endl;
            char writebuffer[512];
            int rc;
            int ipipe = atoi(argv[2]);
            ofstream ofs(argv[1]);
    
    
            while (?)
            {
                rc = read(ipipe, writebuffer, sizeof(writebuffer));
                ofs.write(writebuffer, rc);
            }
    
            close(ipipe);
    
    
    
            return 0;
    }

  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
    while read() returns >0 result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Quote Originally Posted by Salem View Post
    while read() returns >0 result.
    i tried

    Code:
    do
    {
          //do code
    }while (rc>0);
    but my output file was overwritten with nothingness which means it must have written past the end of the file. how can i correct this?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    nvm, i got it. you put me on the right track though, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 06:16 AM
  4. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM