Output File Existence Using fstream

My goal is to input the first line of a file into another file only if the input file exists and the output file doesn't. I have accomplished the status check of the input file, however, my code will still input the line into an already existing file. If the output file already exists, I want the program to terminate. Here is what I have:

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
 #include "main.h"

using namespace std;

int main()
{
        fstream inFile;
        fstream outFile;;
        string fileName("");
        string destName("");

        cout << "Please enter file name: ";
        cin >> fileName;
        cout << endl;

        inFile.open(fileName, ios::in);

        getline(inFile, fileName);

        if (inFile.good() != true) { 
                cout << "File does not exist!\n" << endl; 
                return 0;
        }   

        cout << "Please enter file name of destination: ";
        cin >> destName;
        cout << endl;

        outFile.open(destName, ios::out);    
    
        if (outFile.good() == true) {

                cout << "File '" << destName << "' already exists!\n" << endl;
                return 0;
        }   
    
        outFile.open(destName, ios::out);    
        outFile << fileName << endl;
    

        inFile.close();
        outFile.close();

        return 0;
}


Since I'm trying to learn too, can you please add an exe!planation of what I did wrong? Thank you in advance!
Last edited on
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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string inName;
    cout << "Please enter source file name: ";
    getline(cin, inName);

    ifstream in(inName);
    if (!in.is_open())
    {
        cout << "File does not exist!\n";
        return 0;
    }

    string outName;
    cout << "\nPlease enter destination file name: ";
    getline(cin, outName);


    if (fstream out = fstream(outName))
    {
        cout << "File '" << outName << "' already exists!\n" << endl;
        return 0;
    }

    ofstream out(outName);
    out << inName << '\n';

    // file stream objects close themselves here.
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
        fstream inFile;
        inFile.open("Test.txt");

        if (inFile.good() != true)
		{ 
	        cout << "File does not exist!\n" << endl; 
	        // Ok to open as output file
	    }   
		else
		{
			cout << "File exist!\n" << endl;
	        inFile.close();
		}
        return 0;
}   
Last edited on
I am still very new to fstreams. In class we were told to do ifstream/ofstream. Could you please explain why my

1
2
3
4
5
6
7
8
9
10
11
cin >> destName;
        cout << endl;

        if (outFile.good() != true) {

                cout << "File '" << destName << "' already exists!\n" << endl;
                return 0;
        }   
    
        outFile.open(destName, ios::out);    
        outFile << fileName << endl;


will still write the input file into an already existing file?
See 'Action if file already exists' and 'Action if file does not exist' in
http://en.cppreference.com/w/cpp/io/basic_filebuf/open

1
2
3
4
5
inline bool input_file_exists( std::string path_to_file )
{ return std::ifstream(path_to_file).is_open() ; }

inline bool output_file_exists( std::string path_to_file )
{ return std::ofstream( path_to_file, std::ios_base::in | std::ios_base::out ).is_open() ; }
Topic archived. No new replies allowed.