How would I create an input and ouput file, at runtime if they do not exist?

Below is the code I am playing with (I realize its not working currently)
but wanted to know if there is a good way to accomplish this
Doing a bunch of i/o exercises and I am attempting to create a boilerplate to help with some of my foot work.

In pseudo

1
2
3
4
5
6
7
8
9
10
check for inFile
if Present 
echo that its present
if inFile does not exist
create inFile
echo that its present

repeat for outfile



Code I have been playing around with blindly...
still learning about i/o

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
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>

using namespace std;

int main(int argc, const char * argv[])
{
    
    ifstream inFile;
    inFile.open("inData.txt", ifstream::in);
    if (!inFile.is_open()) {
        inFile.open("inData.txt", ifstream::out);
    }

    
    ofstream outFile;
    
    outFile.open("outData.txt", ifstream::out);
    if (outFile.is_open()) {
        cout << "outFile is open" << endl;
    }
    
    char * dir = getcwd(NULL, 0); // Platform-dependent, see reference link below
    printf("Current dir: %s", dir);
    return 0;
}
Last edited on
closed account (Dy7SLyTq)
if you try to open a file with ofstream and it doesnt exist it will create it.
Topic archived. No new replies allowed.