使用.docx .pdf和.exe扩展名的c ++文件处理。 [英] File handling in c++ with .docx .pdf and .exe extension.

查看:97
本文介绍了使用.docx .pdf和.exe扩展名的c ++文件处理。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想要创建一个文件格式为uptill .txt和wordpad的文件,这段代码是好的,但是当我尝试使用.docx或.pdf或.exe文件时,在打开新文件时出现错误创建文件。



我的意思是,当打开旧的test.docx时,它会在打开新创建的.docx或其他程序时打开例程,出现Word发现错误文件中的不可读内容。



如果是.exe文件,则发生错误的是文件版本与您正在运行的窗口不兼容。



程序成功终止,获得的新文件大小与之前的文件类似,我们在这里看到问题来自打开新文件



我尝试的代码是:

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);
}





任何帮助表示赞赏。



谢谢。



Any help is appreciated.

Thanks.

推荐答案

那是因为.docx,.exe,.pdf文件是二进制文件,而不是文本文件。



请参阅谷歌ifstream读二进制 [ ^ ]
That's because .docx, .exe, .pdf files are binary files, not text files.

see google ifstream read binary[^]


首先 getline 使用新行和回车进行一些处理...因此如提到的那样解决方案1,它不起作用,因为它们不是文本文件。



但即使这样,你的代码也不会复制既不输出那些行分隔符( getline 丢弃行分隔符)。
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).


这篇关于使用.docx .pdf和.exe扩展名的c ++文件处理。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆