GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2007, 09:56
baard baard is offline
New Member
 
Join Date: Sep 2006
Posts: 13
baard is on a distinguished road

Appending file


Hi,
What is wrong with this code.
CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;

char * memblock;


int main () {
	ofstream myfile("newfile.txt",ios::out|ios::binary|ios::app);
	
  ifstream file ("test.txt",ios::in|ios::binary|ios::ate);	
 
  if (file.is_open())
  {
    size = file.tellg();
   
    memblock = new char [size];
    file.seekg (0, ios::beg);
    
	file.read (memblock, size);
 file.close();

  myfile<<memblock<<"adding";

   myfile.close();
 
  cout << "appended \n";
  cout<<size;
    delete[] memblock;

  }
  else cout << "Unable to open file";
  system("pause");
  return 0;
}

There seems to be an array size problem .When I run the program with two text files, I also get some garbage added to the created newfile. When i run the program a second time, more garbage is seen.
How can I properly append two files?
Thanks in advance.
  #2  
Old 08-May-2007, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Appending file


Quote:
Originally Posted by baard
Hi,
What is wrong with this code.
CPP / C++ / C Code:
  char * memblock;
.
.
. 
  memblock = new char [size];
.
.
.   
  file.read (memblock, size);
.
.
.
  myfile<<memblock ...
You allocate an array with exactly as many bytes as the file and read the file into the array. (So far, so good, but you should test the state of the stream after attempting to read.)

Then, when you use the '<<' operator on a variable that is a pointer to char, it expects a null-terminated sequence of bytes. That is because the '<<' operator has been overloaded to treat pointers to char in a way that is consistent with C-style "strings". Since you didn't put a zero byte at the end, then there is no guarantee that there is a zero byte. Furthermore, since you may be reading a binary file, there may be embedded bytes in the file, and the '<<' operator would quit right there (without putting the zero byte in the output file and without going any further). The result might be that not all of the output will be processed.

SO: Even if it isn't a binary file with embedded zeros, if you are going to use file.read() to get the stuff in, why don't you use myfile.write() to write them out? (Or, just read and write a byte at a time with ; why go to the trouble of allocating storage for a buffer?)

Also: You should really test the state of the streams after each read and write operation, as well as after each attempt to open a file.


Regards,

Dave
  #3  
Old 08-May-2007, 10:33
baard baard is offline
New Member
 
Join Date: Sep 2006
Posts: 13
baard is on a distinguished road

Re: Appending file


Thank you very much for the info. Whew, never thought it has such complications. After using write() I was able to append properly. I was trying to create a kind of a resource file .
Thanks again.
  #4  
Old 08-May-2007, 10:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Appending file


Quote:
Originally Posted by baard
T never thought it has such complications.

Doing it isn't complicated, as you have learned. Understanding what really happened and why your first effort didn't work is important. I wish I could go through it with fewer words.


Oh, well...

Regards,

Dave

Footnote: As I mentioned, it's not really necessary to read the entire file into a buffer; just write each byte as you read it. The main part of the program could be as simple as:

CPP / C++ / C Code:
    int size = 0;
    char inchar;
    do {
        inchar = infile.get();
        if (infile) {
            ++size;
            outfile.put(inchar);
        }
    } while (infile);
    cout << "Appended " << size << " bytes from " << inname
         <<  " to " << outname << endl;
Last edited by davekw7x : 08-May-2007 at 11:17.
  #5  
Old 09-May-2007, 06:23
baard baard is offline
New Member
 
Join Date: Sep 2006
Posts: 13
baard is on a distinguished road

Re: Appending file


. I understand. It is always better to know what is happening under the hood.
Will also try the snippet. Thanks again Mr.Dave.
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Problem inserting (appending) in the middle of text file rajeev nair CPP / C++ Forum 3 17-Apr-2007 00:55
Download files in c for windows operating system oozsakarya C Programming Language 5 20-Jun-2006 03:33
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.