How to create a new page on files

Hi guys,

I had a question about files. I wanted to know how to command the text to be outputted on a separate page in the "file.doc" file after each iteration. So for example, in the first iteration, let's say I put the name Kyle. As a result, Kyle would be outputted onto the first of file.doc. Then, in the second iteration, I put the name 'Cassidy.' From there, Cassidy would be outputted onto the page #2 of 'file.doc' and so on and so on. Could someone please show me how to do this if possible? Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<fstream>
using namespace std;

int main()

{
    
    fstream file("file.doc",ios::out);
    string name;
    int num,counter=0;
    char option;
    
    
    do
    {
        counter++;
        cout << "\n\n Iteration #" << counter << endl;
        cout << "Name: ";
        getline(cin,name);
        
        cout << "option (Y/N): ";
        cin >> option; option = toupper(option);
        
        cin.ignore();
        
        file << name << endl << endl;
    }while (option=='Y');
    
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>

int main()
{
    std::ofstream fout("out.doc");
    
    fout << "Kyle";
    fout << (char)12;
    
    fout << "Cassidy";
    fout << (char)12;
    
    fout << "Jimmy";
    fout << (char)12;
}


"Page" is not a well-defined term for a text file, which is what you're currently writing (not an actual Microsoft Word document).
However, it seems Microsoft Word knows how to open a plaintext file with special control characters like Form Feed to mean "new page".

There are ways to programmatically generate Word documents, but I wouldn't say it's a beginner-friendly task.
http://www.cplusplus.com/forum/general/132771/
Duthomhas wrote:
Control an instance of Word using a COM/ActiveX/OpenXML interface.

I recommend you check out libOPC
http://libopc.codeplex.com/

https://www.quora.com/How-can-I-open-a-MS-Word-document-and-write-data-in-it-using-C-or-C++

If you want fancy mark-up, there is also LaTeX, which can be written in plaintext markup and then compiled into a PDF or similar.
https://www.latex-project.org/

_________________________________

If you want to append to a file between program runs without overwriting it, use std::ios::out | std::ios::app in your constructor.
Last edited on
You're not writing a DOC file, you're writing a plain text file named "file.doc". DOC files are binary and have a complex format. It's not something you're prepared to deal with right now.

Now, ASCII provides the character "form feed", which the display device is supposed to interpret as a new page. For example:
1
2
3
4
5
6
7
#include <fstream>

int main(){
    std::ofstream file("output.txt");
    file << "Hello, World!\fGoodbye!";
    return 0;
}
If I open output.txt in OpenOffice, it displays it as a document with two pages.
Sounds great thank you all for your help. I was able to figure it out ! Thanks again

Kyle
Topic archived. No new replies allowed.