File handling in c++ of files having .docx,pdf or .exe extension.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: File handling in c++ of files having .docx,pdf or .exe extension.

  1. #1
    Join Date
    Jan 2014
    Posts
    4

    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.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,512

    Re: File handling in c++ of files having .docx,pdf or .exe extension.

    Use read/write instead of getline and <<

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Jan 2014
    Posts
    4

    Thumbs up Re: File handling in c++ of files having .docx,pdf or .exe extension.

    Quote Originally Posted by 2kaud View Post
    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.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,378

    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 '#'.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jan 2014
    Posts
    4

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center