| | |
Copying a microsoft word doc
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hey AD, in your post (#3) you forgot an ending bracket on this line:
while( fin.read( iobuffer, sizeof(iobuffer) )
Last edited by tux4life; Jul 2nd, 2009 at 7:36 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Hey AD, in your post (#3) you forgot a bracket on this line:
while( fin.read( iobuffer, sizeof(iobuffer) )
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
II have written code that can successfully read and write a microsoft word doc - that is if the word doc contains plain text only. If there are any headings and different fonts used, these are not copied successfully. Which brings me back to my original question - when copying word docs does one need to manipulate the non-ascii chars? And how is this done?
•
•
•
•
When I actually tried it I had the same problem. I used a command prompt and found out that the two files were just a few bytes different.
well, the code I posted almost works. The problem is that the last few bytes does not get read/written
C++ Syntax (Toggle Plain Text)
int main(int argc, char* argv[]) { char iobuf[255]; size_t total = 0; size_t sz = 0; ifstream fin("file1.doc", ios::binary); if( !fin.is_open() ) { cout << "Can't open the file\n"; return 1; } ofstream fout( "copy.doc", ios::binary); while( fin.read(iobuf, sizeof(iobuf) )) { sz = fin.gcount(); total += sz; fout.write(iobuf, sz); sz = 0; } sz = fin.gcount(); if( sz > 0) { cout << "sz = " << sz << "\n"; total += sz; fout.write(iobuf, sz); } fin.close(); fout.close(); cout << "Total = " << total << "\n"; return 0; }
•
•
•
•
II have written code that can successfully read and write a microsoft word doc - that is if the word doc contains plain text only
See the code I already posted and which you quoted. If all you want to do is copy the file then the answer to your question is NO.
Last edited by Ancient Dragon; Jul 3rd, 2009 at 11:55 pm.
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
The microsoft word doc is copied successfully - byte per byte. Copied file is same size as original. However, when I try and use microsoft word to open the copied file, the copied file contents which have non-ascii text are not readable. The ascii text is readable. So the copied file is worthless to the end user if he/she cannot see the non-ascii parts. So I want to be able to copy the file AND open it and read it successfully using ms-word.
•
•
•
•
The microsoft word doc is copied successfully - byte per byte. Copied file is same size as original. However, when I try and use microsoft word to open the copied file, the copied file contents which have non-ascii text are not readable. The ascii text is readable. So the copied file is worthless to the end user if he/she cannot see the non-ascii parts. So I want to be able to copy the file AND open it and read it successfully using ms-word.
I can confirm AD's code works correctly.
(Also tested it on a couple of Word files, playing a bit with the formatting)
The file is loading correctly after copying.
To the OP:
Ensure that you're copying a file which isn't corrupted, before copying you should check whether the file you want to copy loads correctly in MS Word, otherwise you've already missed the boat before the copying process starts.
(Also tested it on a couple of Word files, playing a bit with the formatting)
The file is loading correctly after copying.
To the OP:
Ensure that you're copying a file which isn't corrupted, before copying you should check whether the file you want to copy loads correctly in MS Word, otherwise you've already missed the boat before the copying process starts.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
Attached is the input file - Input.doc and the output file that is created - Output.doc. As you can see outfile looks very different to the inputfile. I used the code that you provided to test this.
•
•
•
•
When I actually tried it I had the same problem. I used a command prompt and found out that the two files were just a few bytes different.
well, the code I posted almost works. The problem is that the last few bytes does not get read/written
C++ Syntax (Toggle Plain Text)
int main(int argc, char* argv[]) { char iobuf[255]; size_t total = 0; size_t sz = 0; ifstream fin("file1.doc", ios::binary); if( !fin.is_open() ) { cout << "Can't open the file\n"; return 1; } ofstream fout( "copy.doc", ios::binary); while( fin.read(iobuf, sizeof(iobuf) )) { sz = fin.gcount(); total += sz; fout.write(iobuf, sz); sz = 0; } sz = fin.gcount(); if( sz > 0) { cout << "sz = " << sz << "\n"; total += sz; fout.write(iobuf, sz); } fin.close(); fout.close(); cout << "Total = " << total << "\n"; return 0; }
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
File is def not corrupted before copying. FYI am using a C++ binary on 2.8 sun solaris operating system. Run the binary and binary creates Output.doc from Input.doc. Output.doc is then ftp'd to desktop where I use miscrosoft word to open it.
Thanks for the help so far.
Thanks for the help so far.
•
•
•
•
I can confirm AD's code works correctly.
(Also tested it on a couple of Word files, playing a bit with the formatting)
The file is loading correctly after copying.
To the OP:
Ensure that you're copying a file which isn't corrupted, before copying you should check whether the file you want to copy loads correctly in MS Word, otherwise you've already missed the boat before the copying process starts.
![]() |
Similar Threads
- write into microsoft word (VB.NET)
- How to insert or embed a html file in to a Word doc ? (Python)
- How to write word doc in linux server with php code. (PHP)
- html to word doc (PHP)
- code for transferring contents of a word doc in a textbox (Visual Basic 4 / 5 / 6)
- Display word doc in vb.net (VB.NET)
- Adding Pictures into Microsoft Word 2003 using Automation (C++)
- Microsoft word starting up in OS 9 (Mac Software)
Other Threads in the C++ Forum
- Previous Thread: g++ linker error. Please help.
- Next Thread: [C++] Problem with changing contents of char[]
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






