| | |
Copying a microsoft word doc
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
I have 2 binaries - a java binary that requests a microsoft word doc from a c++ binary. The C++ binary opens the word doc in binary mode, reads x no of chars and returns chars to java binary. Java binary eventually receives all data and writes data using filestream write. When I try to open the newly created file, the contents are not readable. The size of the newly created file is the exact same size as the original file that is read by the C++ server.
Should the java and C++ binaries try and manipulate the microsoft word line feeds etc?
Should the java and C++ binaries try and manipulate the microsoft word line feeds etc?
I once tried to do something like this with ONLY C++. Tried to take all the contents of a document and remake it with the same thing. The problem is though that there are some characters that may not show up (may not follow ascii char set) and their may be text that is not being retrieved. Make sure your getting all the text, so use a pointer:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; char* main(char* file) { char* contents; char* buffer; int numOfChars = 0; char ch; ifstream fin(file); if(fin) { while(fin.get(ch)) { buffer = new char[numOfChars+2]; for(int i = 0; i < numOfChars; i++) buffer[i] = contents[i]; buffer[numOfChars] = ch; buffer[numOfChars+1] = '\0'; delete contents; contents = new char[++numOfChars+1]; for(int i = 0; i < numOfchars; i++) contents[i] = buffer[i]; contents[numOfChars] = '\0'; delete buffer; } fin.close(); } else return "Error"; return contents; }
Last edited by u8sand; Jul 1st, 2009 at 10:29 pm.
>>When I try to open the newly created file, the contents are not readable
Its because doc files are binary files, not text files. Those files contain a lot of formatting information, such as font, font color, font size, etc, that is only readable by MS-Word or similar compatible program.
Binary files have to be opened in binary mode
Its because doc files are binary files, not text files. Those files contain a lot of formatting information, such as font, font color, font size, etc, that is only readable by MS-Word or similar compatible program.
Binary files have to be opened in binary mode
ifstream fin(file, ios::binary); and use stream's read() method. C++ Syntax (Toggle Plain Text)
ifstream fin(file, ios::binary); ofstream out("newfile.doc", ios::binary); char iobuffer[255]; while( fin.read( iobuffer, sizeof(iobuffer) ) { // do something with this block of data size_t sz = fin.gcount(); out.write( iobuffer, sz); }
Last edited by Ancient Dragon; Jul 1st, 2009 at 10:37 pm.
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
I'm pretty sure that the text is being copied correctly insofar as one can using C++ filestream reads/writes and buffers. File sizes are the same also. Does one need to use microsoft apis to ensure that non-ascii chars are converted?
•
•
•
•
I once tried to do something like this with ONLY C++. Tried to take all the contents of a document and remake it with the same thing. The problem is though that there are some characters that may not show up (may not follow ascii char set) and their may be text that is not being retrieved. Make sure your getting all the text, so use a pointer:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; char* main(char* file) { char* contents; char* buffer; int numOfChars = 0; char ch; ifstream fin(file); if(fin) { while(fin.get(ch)) { buffer = new char[numOfChars+2]; for(int i = 0; i < numOfChars; i++) buffer[i] = contents[i]; buffer[numOfChars] = ch; buffer[numOfChars+1] = '\0'; delete contents; contents = new char[++numOfChars+1]; for(int i = 0; i < numOfchars; i++) contents[i] = buffer[i]; contents[numOfChars] = '\0'; delete buffer; } fin.close(); } else return "Error"; return contents; }
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
I missed this reply - sorry. I am treating the microsoft word doc in the C++ code as a binary doc and using fstreams to read/write the data. Then when I use microsoft word to open the newly copied file, the contents are not readable.
Is it possible to just read the contents of the microsoft doc file in binary form, write and open without doing any formatting of special chars?
Is it possible to just read the contents of the microsoft doc file in binary form, write and open without doing any formatting of special chars?
•
•
•
•
I missed this reply - sorry. I am treating the microsoft word doc in the C++ code as a binary doc and using fstreams to read/write the data. Then when I use microsoft word to open the newly copied file, the contents are not readable.
Is it possible to just read the contents of the microsoft doc file in binary form, write and open without doing any formatting of special chars?
•
•
•
•
I once tried to do something like this with ONLY C++. Tried to take all the contents of a document and remake it with the same thing. The problem is though that there are some characters that may not show up (may not follow ascii char set) and their may be text that is not being retrieved. Make sure your getting all the text, so use a pointer:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; char* main(char* file) { char* contents; char* buffer; int numOfChars = 0; char ch; ifstream fin(file); if(fin) { while(fin.get(ch)) { buffer = new char[numOfChars+2]; for(int i = 0; i < numOfChars; i++) buffer[i] = contents[i]; buffer[numOfChars] = ch; buffer[numOfChars+1] = '\0'; delete contents; contents = new char[++numOfChars+1]; for(int i = 0; i < numOfchars; i++) contents[i] = buffer[i]; contents[numOfChars] = '\0'; delete buffer; } fin.close(); } else return "Error"; return contents; }
int main() and not char* main() or void main() or ... ?? Last edited by tux4life; Jul 2nd, 2009 at 6:24 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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
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; }
Last edited by Ancient Dragon; Jul 2nd, 2009 at 6:37 am.
![]() |
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 code coding compile compiler console conversion 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 integer 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 struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






