![]() |
|
#1
|
|||
|
|||
Appending fileHi,
What is wrong with this code. CPP / C++ / C Code:
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
|
|||
|
|||
Re: Appending fileQuote:
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
|
|||
|
|||
Re: Appending fileThank 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
|
|||
|
|||
Re: Appending fileQuote:
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:
Last edited by davekw7x : 08-May-2007 at 11:17.
|
|
#5
|
|||
|
|||
Re: Appending fileWill also try the snippet. Thanks again Mr.Dave. |
Recent GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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