-3

I was trying to output a string to a file using File I/O of C++, but I decided to change the extension of the output file to .pdf, .docx and so on.

None of them seem to open up.

So are there any more internal translations required in order to make that file a proper(file which opens) pdf or docx??

void convert(char *file){

    ifstream in(file);
    ofstream out("out.pdf",ios::out);

    char str;

    while(in && out){
        in.get(str);
        out.put(str);
    }

    in.close();
    out.close();
    cout<<"Done!!!";
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Rishu
  • 95
  • 1
  • 5
  • 6
    File extensions mean nothing, they are at best a hint to the OS as to which application it should open when you double click on a file in the file explorer. – Borgleader Jul 07 '17 at 18:36
  • so, is it just a .txt file which would open up normally? – Rishu Jul 07 '17 at 18:39
  • The file formats you mentioned are not equal. Each one has its own format specification. A valid PDF file (one that works when you open it) has to follow the PDF format specification, otherwise editor/reader applications won't be able to parse it. A txt file, on the other hand, is pure text, so editors/readers don't need any extra effort to show its contents (except when dealing with encoding, BOM, etc). – jweyrich Jul 07 '17 at 18:39
  • You need to read the detailed specification of the format of PDF files, then write code to produce a file in that format. There might be a library to help with this. – Barmar Jul 07 '17 at 18:39
  • 3
    ***So are there any more internal translations required in order to make that file a proper(file which opens) pdf or docx??*** Yes and for pdf it will be months to years of work for you to implement this if you succeed. – drescherjm Jul 07 '17 at 18:40
  • 2
    ***so, is it just a .txt file which would open up normally?*** pdf is much more complex than that. – drescherjm Jul 07 '17 at 18:40
  • a plain .txt file doesn't need any translations?? – Rishu Jul 07 '17 at 18:43
  • ***a plain .txt file doesn't need any translations??*** It does not. – drescherjm Jul 07 '17 at 18:44
  • thanks @drescherjm – Rishu Jul 07 '17 at 18:47
  • Next time, give more motivation in your question. I tried to guess (apparently with success, since you accepted my answer), but when asking something here you should give some context & motivation. – Basile Starynkevitch Jul 07 '17 at 18:56
  • sure @BasileStarynkevitch – Rishu Jul 07 '17 at 18:57

2 Answers2

3

PDF is (and also docx which actually is OOXML; and of course also OpenDocument & DocBook) a file format (and quite a complex one), specified in a complex English document (the ISO 32000 standard) defining which sequences of bytes are valid (and what they represent).

Grossly speaking a PDF file contains elementary steps to draw ink on paper or pixels on screen. With gross simplification, these steps include things like "move the pen to position x=23 y=50", "choose the Arial font of 10pt", "draw the word abc in that font at that position", "choose the pink color for the pen" etc etc, but the details are much more complex.

File extensions don't mean anything (except as an important convention).

To generate a PDF file, you either need to spend weeks or months to study the specification of that PDF format, or to use some library for that (see this question, and also podofo, poppler and several other libraries). Even with the help of a library, you need to understand something about that format.

Are there any more internal translations required in order to make that file

I'm not sure to understand what you mean by translation (a better word would be "converter"). You could generate PDF by sending some other content to a suitable program emitting PDF. You might consider using some document formatter (like LaTeX thru pdflatex, or Lout, or some older variant of troff, etc..) which you would feed with a higher-level file format containing text formatting directives mixed with (some encoding of) the text to be formatted. On Unix like systems you might even use some command pipeline (to avoid writing some "temporary" file), perhaps using popen(3) and related functions.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

To write a pdf, docx, etc. you need to setup a file header, and format the data correctly. Changing the extension does not really do much if anything at all.

You will have to use an outside an outside library or format the code yourself. Below are a couple of examples of extensions you can use:

PDF docx

gmwagner
  • 71
  • 4