File handling in c++ of files having .docx,pdf or .exe extension.
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:
#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);
}</pre>
Any help is appreciated.
Thanks.
Re: File handling in c++ of files having .docx,pdf or .exe extension.
Use read/write instead of getline and <<
Re: File handling in c++ of files having .docx,pdf or .exe extension.
getline() assumes the stream is divided up into lines which are separated by '\n' or another specified character. Binary streams are not so divided and so getline() is not appropriate.
As mentioned by Alex in post #2, read() is the better function to use as it will read the specified number of chars from the stream (or until eof is reached).
See http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx for more details.
Note that outputing non-printable characters to a display can cause weid results!
Re: File handling in c++ of files having .docx,pdf or .exe extension.
Quote:
Originally Posted by
2kaud
getline() assumes the stream is divided up into lines which are separated by '\n' or another specified character. Binary streams are not so divided and so getline() is not appropriate.
As mentioned by Alex in post #2, read() is the better function to use as it will read the specified number of chars from the stream (or until eof is reached).
See
http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx for more details.
Note that outputing non-printable characters to a display can cause weid results!
Thanks alot It is working for .exe file by using read and write instructions butt I am still facing problem with .docx files.
Please do something for .docx too.
Re: File handling in c++ of files having .docx,pdf or .exe extension.
Please post the curent code you are using and explain the problem you are having with .docx files. When posting code, please format properly before posting and use code tags. Go Advanced, select the code and then click '#'.
Re: File handling in c++ of files having .docx,pdf or .exe extension.
Thanks alot I got it.It says that the word file is corrupted but when I press the button to recover it then I got original file.
Thank you very much.:thumb: