Writing data to specified file and directory

In my code I create a folder in a specified path and open a file for writing. I then pass the outputfile to a function to do the actual writing. However, I can write to the file in main but not in my function.
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
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;
void checkheader(ostream& outputFile)
{
outputFile << "please work already " << endl; //write this to csv for practice
}
void CreateFolder(const char * path)
{
	if (!CreateDirectory(path, NULL))
	{
		return;
	}
}

int main(void)
{
        CreateFolder("C:\\CSV_ERROR_FILES_HERE\\");// create folder in specified path
	string filename = "errors.csv"; // create filename
	ofstream outputFile; 
	outputFile.open("C:/CSV_ERROR_FILES_HERE/errors.csv"); //open to outputfile for writing
        checkheader(outputFile); // call the function


}
	
It would seem some error checking is in order. I can't test this since I don't use Windows, but maybe something like this:

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
#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;

void checkheader(ostream& fout) {
    fout << "please work already\n";
}

bool CreateFolder(const char *path) {
    if (!CreateDirectory(path, 0) && GetLastError() != ERROR_ALREADY_EXISTS)
        return false;
    return true;
}

int main() {
    if (!CreateFolder("C:\\CSV_ERROR_FILES_HERE\\")) {
        cerr << "Problem creating folder\n";
        return -1;
    }

    ofstream fout("C:/CSV_ERROR_FILES_HERE/errors.csv");
    if (!fout) {
        cerr << "Problem opening file\n";
        return -1;
    }

    checkheader(fout);
}

Topic archived. No new replies allowed.