Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
The question I have is that I want to create a file with any file format uptill .txt and wordpad this code is Ok, Butt when I try it for .docx or .pdf or .exe files the error occures in opening new created file.

I means that while opening the older test.docx it open as in routine while opening newly created .docx or others, an error occurs that "Word found unreadable content in file".

and in case of .exe file the error occured is "The version of file is not compatible with window you are running ".

The program is terminated successfully and the size of new file obtained is similar to previous one from where we read the problem comes in opening new file

The code I tried is:
C++
#include <iostream;>
#include<fstream;>
using namespace std;
#include<string;>

int main()
{
    string line;
    ifstream in;
    in.open("test.docx",ios_base::binary);
    ofstream out;
    out.open("test1.docx",ios_base::binary);
     if (in.is_open())
  {
      while ( !in.eof() )
    {
        getline(in,line);
      out<<line;
      cout<<line;
    }


  }
     out.close();
    in.close();

    system("PAUSE");
    return (0);
}


Any help is appreciated.

Thanks.
Posted
Updated 31-Dec-13 22:57pm
v2
Comments
Jochen Arndt 1-Jan-14 5:29am    
Are you really sure that the file sizes are identical? The getline() function stops reading when a newline character is read without storing it in the buffer.

If you want to copy binary files, you should not use character based functions like getline() but low level read and write functions.
[no name] 1-Jan-14 6:27am    
Exactly
Nelek 1-Jan-14 6:36am    
My 5

That's because .docx, .exe, .pdf files are binary files, not text files.

see google ifstream read binary[^]
 
Share this answer
 
First of all getline does some handling with new line and carriage returns... Thus as mentionned in solution 1, it won't work because they are not text file.

But even then, you code does not copy neither as you don't output those line separators (getline discards line separators).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900