Creating Output(n), when N is an increasing number, when Output.txt exists.

Hello!

The title basic describes my problem, and I'm struggling to work out how to do such a thing, I've looked at stringstreams amongst other ways but cannot get it to work.

This is the code to my program, its a word-list generator.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* 
 * File:   main.cpp
 * Author: Damien
 *
 * Created on 05 September 2011, 23:46
 */

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <time.h>


using namespace std;
int UserChoice();
double timeConvert();
void Greeting();
double TimeTaken(clock_t clock2,clock_t clock1);
string iDirectory;
string wBuffer;
int a; 
int uNum;

int main() {
    Greeting();
    cout << endl;
    a = UserChoice();
    if (a==5) {
        cout << "Enter the number X you wish to append: " << endl;
        cin >> uNum;
    }
    fstream myFile(iDirectory.c_str());
    if (myFile.fail()) {
        myFile.clear(ios::failbit);
        cout << "That file does not exist. Please re-enter the file location: " << endl;
        cin >> iDirectory;
    }
    ofstream ouFile ("output.txt"); 
    clock_t begin=clock();
    while (!myFile.eof())
    {
    if(a==1) {   
        myFile >> wBuffer;
        wBuffer[0] = toupper(wBuffer[0]);        
        ouFile << wBuffer << "1" << endl;
        }
    else if (a==2) {
        myFile >> wBuffer;
        wBuffer[0] = toupper(wBuffer[0]);        
        ouFile << wBuffer << "12" << endl;
    }
    else if (a==3) {
        myFile >> wBuffer;
        wBuffer[0] = toupper(wBuffer[0]);
        ouFile << wBuffer << "123" << endl;
    }
    else if (a==4) {
        myFile >> wBuffer;
        wBuffer[0] = toupper(wBuffer[0]);
        ouFile << wBuffer << "1" << endl;
        ouFile << wBuffer << "12" << endl;
        ouFile << wBuffer << "123" << endl;
    }   
    else if (a==5) {        
        myFile >> wBuffer;
        int length = wBuffer.length();
        for(int i=0; i!=length; i++){
            wBuffer[i] = toupper(wBuffer[i]);
        }
        ouFile << wBuffer << uNum << endl;                
    }
    }
    cout << endl;
    clock_t end=clock();
    ouFile.close();
    string dir;
    ShellExecute(NULL, "open", (dir + "\\output.txt").c_str(), NULL, dir.c_str(), SW_SHOWNORMAL);
    cout << "Time Elapsed: " << (TimeTaken(end,begin)/1000) << " Seconds." << endl;
    cout << "Do you want to generate any other Word-Lists? YES OR NO: " << endl;
    string response;
    cin >> response;
    if (response == "YES") {
        UserChoice();
    }
    else if (response == "NO") {
    cout << "Success. Output File Located in the same Directory as this tool. Press ENTER to continue..." << endl;
    cin.ignore();cin.get();
    return 0;
    }
}

int UserChoice() {
    int x;
    cout << "Please choose from the following options: " << endl;
    cout << "1. Append 1 to the end of each and every word and Capitalise first char" << endl;
    cout << "2. Append 12 to the end of each and every word and Capitalise first char" << endl;
    cout << "3. Append 123 to the end of each and every word and Capitalise first char." << endl;
    cout << "4. Append 1, then 12, then 123 to each and every word and Capitalise first char." << endl;
    cout << "5. Capitalise all Chars in each Word, and append Number of X specified by User" << endl;
    cout << "6. Exit." << endl;
    cin >> x;
    if(x==6){
      cout << "Thank-You for using Word-List Generator V1.1!" << endl;
      cout << endl;
      cout << "Press ENTER to continue..." << endl;
    cin.ignore();cin.get();
    exit(1);
  }         
    cout << endl;
    cout << "You chose option: " << x << endl;   
    cout << endl;      
    cout << "Please enter the Word-List you wish to open (C:/..): " << endl;
    cin >> iDirectory;
    return x;
}

void Greeting() {
    const string greeting = "Welcome to the Word List Generator V1.1!";
    const int pad = 1;
    const int rows = pad * 2 + 3;
    const string::size_type cols = greeting.size() + pad * 2 + 2;
    cout << endl;
    for (int r = 0; r != rows; r = r + 1 ) {
        string::size_type c = 0;
        while (c != cols) {
            if (r == pad + 1 && c == pad + 1) {
                cout << greeting;
                c += greeting.size();
            } else {
                if (r == 0 || r == rows - 1 ||
                    c == 0 || c == cols - 1)
                    cout << "*";
                else
                    cout << " ";
                c = c + 1 ;
            }
        }
        cout << endl;
    }

}

double TimeTaken(clock_t clock2,clock_t clock1) {
    double diffticks = clock2-clock1;
    double diffms = (diffticks*1000)/CLOCKS_PER_SEC;
    return diffms;
}


Essentially, here, in this part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
cout << "That file does not exist. Please re-enter the file location: " << endl;
        cin >> iDirectory;
    }
    ofstream ouFile ("output.txt"); 
    clock_t begin=clock();
    while (!myFile.eof())
    {
    if(a==1) {   
        myFile >> wBuffer;
        wBuffer[0] = toupper(wBuffer[0]);        
        ouFile << wBuffer << "1" << endl;
        }


After ofstream ouFile ("output.txt"); I want to check whether Output.txt exists, then, if it does, create Output1.txt, then Output2.txt and so forth, if it doesn't exist, create Output.txt as standard.

Currently, thats where I think it should be placed, but maybe not, all help is appreciated.
Bump.
The only portable way to do this that I am aware of would be attempting to create an ifstream with that filename and check if it succeeds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string fileBase("output");
string fileString;
ifstream fileCheck(fileBase+".txt");
int n=1;
stringstream ss;
while(fileCheck.fail())
{
fileCheck.clear();
ss<<fileBase<<n<<".txt";
getline(ss,fileString);
fileCheck.open(fileString);
++n;
}
fileCheck.close();

ofstream actualFile(fileString);


Didn't check, but I think this should work.
Last edited on
Topic archived. No new replies allowed.