File Output Problem.

I am a beginner at C++ programming. I am trying to write a code that will read from a user-specified input file and write any vowels or newline characters in the file to an output file called "vowels_[input file name]". The code below seems to work for opening and creating the files, but it is not outputting correctly for some reason. Every different configuration of output methods I have tried all result in a blank output file. What am I missing here?

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string fileNameS;
    char *fileNameC;
    bool flag = false;
    char c = ' ';

    do
    {
        //Get file name.
        cout << "Please enter file name, then press enter: ";
        cin >> fileNameS;
        //Convert name to *char for fstream use.
        fileNameC = new char[fileNameS.length() + 1];
        strcpy(fileNameC, fileNameS.c_str());
        //Check that the name was entered properly, otherwise repeat loop.
        ifstream test(fileNameC);
        if(test)
            flag = true;
        else
            cout << endl << fileNameC << " does not exist." << endl << endl;
    }while(!flag);

    //Open the file for reading.
    ifstream ifile(fileNameC);
    ifile.open(fileNameC, ios::in);
    //Clear memory.
    delete[] fileNameC;

    //Create new file name for output, and convert to *char for fstream use.
    fileNameS = "vowels_" + fileNameS;
    fileNameC = new char[fileNameS.length() + 1];
    strcpy(fileNameC, fileNameS.c_str());

    //Open file for writing.
    ofstream ofile(fileNameC);
    ofile.open(fileNameC, ios::out);
    //Clear memory.
    delete[] fileNameC;

    //Loop through input file char by char.
    while(ifile.get(c))
    {
        //If the current char is a vowel or newline, write to output file.
        if(c == ('a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U'||'\n'))
            ofile.put(c);
    }
    //Close files.
    ifile.close();
    ofile.close();

    return 0;
}


Sample input file: "Input.txt"

abcdef
ghijkl
mnopqr
stuvwx
yz
For one, your check on line 52 is wrong. You evaluate ('a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U'||'\n') which is true, by the way, and compare it to c. I think you meant to check if c is any of those characters, which is more like c == 'a' || c == 'A' || ...
Last edited on
Ah, thanks for that. However, regardless of what I put in the while loop, nothing is written to the output. For example, if I write:

1
2
3
4
while(ifile.get(c))
{
     ofile.put(c);
}


or even:

1
2
3
4
while(ifile.get(c))
{
     ofile << c;
}


I still get a blank output file.

In fact, even if I write:
1
2
while(ifile.get(c))
     cout << c;


I cant even see output to the console.
Last edited on
One tip: don't keep dynamically constructing those C-strings to pass around. Just give the file streams the strings directly or .c_str() if you aren't using the latest version of C++.

With respect to your actual problem, I'd only open the file once; just check if it worked and if it does, use that stream rather than opening a second which could fail separately.
Updated with your suggestions, code still not working. Thanks for the efficiency tips tho, I made it more complex than it needed to be lol.

New code:
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string fileName;
    bool flag = false;
    char c = ' ';

    do
    {
        //Get file name.
        cout << "Please enter file name, then press enter: ";
        cin >> fileName;
        //Check that the name was entered properly, otherwise repeat loop.
        ifstream ifile(fileName.c_str());
        if(ifile)
            flag = true;
        else
            cout << endl << fileName << " does not exist." << endl << endl;
    }while(!flag);

    //Open the file for reading.
    ifstream ifile(fileName.c_str());
    ifile.open(fileName.c_str(), ios::in);

    //Create file name for output.
    fileName = "vowels_" + fileName;

    //Open file for writing.
    ofstream ofile(fileName.c_str());
    ofile.open(fileName.c_str(), ios::out);

    //Loop through input file char by char.
    while(ifile.get(c))
    {
        //If the current char is a vowel or newline, write to output file.
        if(c == 'a'||c == 'A'||c =='e'||c =='E'||c =='i'||c =='I'||c =='o'||c =='O'||c =='u'||c =='U'||c =='\n')
            ofile.put(c);
    }
    //Close files.
    ifile.close();
    ofile.close();

    return 0;
}


Still gettting a blank output file.

Side note: If I try to only declare the ifstream once (in the initial loop to test) i get the error: "ifile was not declared in this scope". Is there another way for me to test the user-input without declaring a second ifstream such as in your suggestion?
Last edited on
Any other suggestions?
You could declare just one fstream and instead of using the constructor to open the file, use .open().

On that note, now that I've looked again, you are opening the files twice! The first time in the constructor and again immediately after with open(). Try removing the unnecessary calls to open().
Hallelujah! You solved it. Idk why I thought I had to use .open() after declaring the fstream. Thanks so much!
For anyone interested, here is the final working code:

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string fileName;
    bool flag = false;
    char c = ' ';

    do
    {
        //Get file name.
        cout << "Please enter file name, then press enter: ";
        cin >> fileName;
        //Check that the name was entered properly, otherwise repeat loop.
        ifstream test(fileName.c_str());
        if(test)
            flag = true;
        else
            cout << endl << fileName << " does not exist." << endl << endl;
    }while(!flag);

    //Open the file for reading.
    ifstream ifile(fileName.c_str());

    //Create file name for output.
    fileName = "vowels_" + fileName;

    //Open file for writing.
    ofstream ofile(fileName.c_str());

    //Loop through input file char by char.
    while(ifile.get(c))
    {
        //If the char is a vowel or newline, write to output file.
        if((c == 'a')||(c == 'A')||(c =='e')||(c =='E')||(c =='i')||(c =='I')||(c =='o')||(c =='O')||(c =='u')||(c =='U')||(c =='\n'))
            ofile.put(c);
    }
    //Close files.
    ifile.close();
    ofile.close();

    return 0;
}
Topic archived. No new replies allowed.