I have a program that splits up a single .txt file into separate files (one per sales rep). The original text file has a layout that used to be printed on wide tractor feed paper. I am saving these output files as .doc's. My only problem is that, when our sales reps open these, they have to set the page orientation to landscape, put the font to 8pt, and view at 150% and then it looks perfect. Is there any way to automatically save this way from c++? I will attach my code below just in case.

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;
string getInFile();
void printProgHeading();
int main(int argc, char *argv[])
    string filName = getInFile();
    ifstream inFile (filName.c_str());
    ofstream *outFile = NULL;
    int numFiles = 0;
    string line = "";
    string placeHolder = "";
    bool isHeaderLine = false; 
    int wordCount = 0;
    string currSalesman = "";
    if (inFile.is_open()){
        system("mkdir splits");
        while (! inFile.eof()){

            getline (inFile,line);
            isHeaderLine = false;
            wordCount = 0;
            istringstream iss(line);
            string token;
            while(getline(iss, token, ' ')){
                if(token!=" " and token != "") {
                    if(token=="Salesman") { 
                        isHeaderLine = true;
                        if (wordCount == 6){
                            if (token != currSalesman) {  
                                currSalesman = token;
                                token = "splits/" + token + ".rtf";
                                cout << "Creating file " << token << endl;
                                outFile = new ofstream(token.c_str());       
            if(outFile != NULL) *outFile << line << endl;                      
        cout << "Unable to open file\n"; 
    return EXIT_SUCCESS;

string getInFile(){
    string subString1 = "type ";
    string subString2 = " > ";
    string subString3 = "c.txt";
    string subString4 = ".txt";
    string filName = "";
    string finalString = "";
    cout << "Enter file name: ";
    cin >> filName;
    finalString = subString1 + filName + subString4 + subString2 + filName + subString3; 
    cout << "Running operation: " << finalString << endl << endl;
    cout << "Please be patient while file is being converted from unicode..." << endl;
    system (finalString.c_str());
    cout << "File conversion complete." << endl << endl;
    cout << "Please hold while files are being created." << endl << endl;
    filName = filName + subString3;
    return filName;  

void printProgHeading(){
     cout << "****************************************************************\n"
          << "** Author: David MacKay                                       **\n"
          << "** ~Program splits the Follow-Up Review Report into separate  **\n"
          << "**  files for each sales person.  Please contact David MacKay **\n"
          << "**  with any questions regarding the operation of this        **\n"
          << "**  program.                                                  **\n"
          << "****************************************************************\n"
          << endl
          << "----------------------------------------------------------------\n";

Recommended Answers

Read this article. You will probably want to write the code with VB Automation.

and an FAQ

Jump to Post

All 2 Replies

You may achieve the same goal in C++ without cumbersome Word automation. Try to generate HTML report with a proper style sheet for media print. HTML files are simple text files so your report generator is an ordinar console application. No need in Word installation. Moreover, no need in Windows, it's an extreme portable approach. After that an user can open these HTML reports in Word and print them. MS Word processes such CSS sheets (page breaks, font setting etc) correctly.

Regrettably it's not a 1-page code and you need at least basic knowledges in HTML and CSS. I have used this approach, it works perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.