943,791 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2476
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 1st, 2009
0

Copying a microsoft word doc

Expand Post »
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?
Similar Threads
Reputation Points: 6
Solved Threads: 0
Newbie Poster
shealy is offline Offline
14 posts
since Jun 2009
Jul 1st, 2009
0

Re: Copying a microsoft word doc

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)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. char* main(char* file)
  7. {
  8. char* contents;
  9. char* buffer;
  10. int numOfChars = 0;
  11. char ch;
  12. ifstream fin(file);
  13. if(fin)
  14. {
  15. while(fin.get(ch))
  16. {
  17. buffer = new char[numOfChars+2];
  18. for(int i = 0; i < numOfChars; i++)
  19. buffer[i] = contents[i];
  20. buffer[numOfChars] = ch;
  21. buffer[numOfChars+1] = '\0';
  22. delete contents;
  23. contents = new char[++numOfChars+1];
  24. for(int i = 0; i < numOfchars; i++)
  25. contents[i] = buffer[i];
  26. contents[numOfChars] = '\0';
  27. delete buffer;
  28. }
  29. fin.close();
  30. }
  31. else
  32. return "Error";
  33. return contents;
  34. }
Last edited by u8sand; Jul 1st, 2009 at 10:29 pm.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 1st, 2009
0

Re: Copying a microsoft word doc

>>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 ifstream fin(file, ios::binary); and use stream's read() method.
C++ Syntax (Toggle Plain Text)
  1. ifstream fin(file, ios::binary);
  2. ofstream out("newfile.doc", ios::binary);
  3. char iobuffer[255];
  4. while( fin.read( iobuffer, sizeof(iobuffer) )
  5. {
  6. // do something with this block of data
  7. size_t sz = fin.gcount();
  8. out.write( iobuffer, sz);
  9. }
Last edited by Ancient Dragon; Jul 1st, 2009 at 10:37 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

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?




Click to Expand / Collapse  Quote originally posted by u8sand ...
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)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. char* main(char* file)
  7. {
  8. char* contents;
  9. char* buffer;
  10. int numOfChars = 0;
  11. char ch;
  12. ifstream fin(file);
  13. if(fin)
  14. {
  15. while(fin.get(ch))
  16. {
  17. buffer = new char[numOfChars+2];
  18. for(int i = 0; i < numOfChars; i++)
  19. buffer[i] = contents[i];
  20. buffer[numOfChars] = ch;
  21. buffer[numOfChars+1] = '\0';
  22. delete contents;
  23. contents = new char[++numOfChars+1];
  24. for(int i = 0; i < numOfchars; i++)
  25. contents[i] = buffer[i];
  26. contents[numOfChars] = '\0';
  27. delete buffer;
  28. }
  29. fin.close();
  30. }
  31. else
  32. return "Error";
  33. return contents;
  34. }
Reputation Points: 6
Solved Threads: 0
Newbie Poster
shealy is offline Offline
14 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

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?
Reputation Points: 6
Solved Threads: 0
Newbie Poster
shealy is offline Offline
14 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

>>Does one need to use microsoft apis to ensure that non-ascii chars are converted?

Huh? I didn't post anything specific to microsoft, only standard C++ stuff. Binary files have to be opened in binary mode using ios::binary option. If you don't do that then the destination file will be corrupt.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

Click to Expand / Collapse  Quote originally posted by shealy ...
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?
That is exactly what the code snipped I posted will do. Its just standard file i/o operation, nothing special about it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

To be clear - I am using fstreams and read and opening the microsoft word doc in binary mode. Ditto with the newly created file that gets the contents of the word doc. All this is done using C++ code. When I try to view the newly created doc with ms-word, the contents are not readable.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
shealy is offline Offline
14 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

Click to Expand / Collapse  Quote originally posted by u8sand ...
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)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. char* main(char* file)
  7. {
  8. char* contents;
  9. char* buffer;
  10. int numOfChars = 0;
  11. char ch;
  12. ifstream fin(file);
  13. if(fin)
  14. {
  15. while(fin.get(ch))
  16. {
  17. buffer = new char[numOfChars+2];
  18. for(int i = 0; i < numOfChars; i++)
  19. buffer[i] = contents[i];
  20. buffer[numOfChars] = ch;
  21. buffer[numOfChars+1] = '\0';
  22. delete contents;
  23. contents = new char[++numOfChars+1];
  24. for(int i = 0; i < numOfchars; i++)
  25. contents[i] = buffer[i];
  26. contents[numOfChars] = '\0';
  27. delete buffer;
  28. }
  29. fin.close();
  30. }
  31. else
  32. return "Error";
  33. return contents;
  34. }
Hey, why has no-one told him that it's int main() and not char* main() or void main() or ... ??
Last edited by tux4life; Jul 2nd, 2009 at 6:24 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 2nd, 2009
0

Re: Copying a microsoft word doc

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)
  1. int main(int argc, char* argv[])
  2. {
  3. char iobuf[255];
  4. size_t total = 0;
  5. size_t sz = 0;
  6. ifstream fin("file1.doc", ios::binary);
  7. if( !fin.is_open() )
  8. {
  9. cout << "Can't open the file\n";
  10. return 1;
  11. }
  12. ofstream fout( "copy.doc", ios::binary);
  13. while( fin.read(iobuf, sizeof(iobuf) ))
  14. {
  15. sz = fin.gcount();
  16. total += sz;
  17. fout.write(iobuf, sz);
  18. sz = 0;
  19. }
  20. sz = fin.gcount();
  21. if( sz > 0)
  22. {
  23. cout << "sz = " << sz << "\n";
  24. total += sz;
  25. fout.write(iobuf, sz);
  26. }
  27. fin.close();
  28. fout.close();
  29. cout << "Total = " << total << "\n";
  30. return 0;
  31. }
Last edited by Ancient Dragon; Jul 2nd, 2009 at 6:37 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: g++ linker error. Please help.
Next Thread in C++ Forum Timeline: [C++] Problem with changing contents of char[]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC