ofstream txt file creation..

Hey all,

With ofstream, i'm trying to create and write to a txt file but it doesn't appear to be working. No errors, just cannot find the file its created..

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// FUNCTION: SAVE MELODIES TO A TXT FILE..
void SAVE_TXT()
{
	string TXT_NAME;	// String value for name of txt file to save to..

	cout << "Please enter the desired txt file name: " << endl;
	cin >> TXT_NAME;

	string FULL_TXT_PATH = ( "../MIDI & TXT Files/" + TXT_NAME + ".txt");
	cout << "Filename: " << FULL_TXT_PATH;
	ofstream TXT_FILE (FULL_TXT_PATH, ofstream::out);
	TXT_FILE << "Has this saved properly?";
}
// END FUNCTION.. 


I'm trying to create it in a folder inside my current project called "MIDI & TXT Files" but the file is not there, nor is it anywhere on my laptop.

I'm assuming i have syntax issues in my path of something. Could someone let me know what i'm missing or doing wrong please? :)

Paul..
Last edited on
"../MIDI & TXT Files/"
is not a valid path in Windows. Try without the ampersand.
You should add some file status checking to determine what is actually happening.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    ofstream TXT_FILE (FULL_TXT_PATH);
    if (!TXT_FILE)
    {
        cout << "\nError opening output file\n";
        return; 
    }
	
    TXT_FILE << "Has this saved properly?";
    if (TXT_FILE)
    {
        cout << "\nWrite was successful\n";
    }
    else
    {
        cout << "\nWrite Failed\n";	    
    }
Last edited on
Hey Thomas and Chervil, thanks for your replies and help :)

I've used both your ideas and have this so far:
(FORMAT_TXT() is just a function called which prepares how the info is going to look)

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
// FUNCTION: SAVE MELODIES TO A TXT FILE..
void SAVE_TXT()
{
	string TXT_NAME;	// String value for name of txt file to save to..

	cout << "\nPlease enter the desired txt file name: ";
	cin >> TXT_NAME;

	string FULL_TXT_PATH = ( "../MIDI_and_TXT_Files/" + TXT_NAME + ".txt");
	cout << "Filename: " << FULL_TXT_PATH;
	ofstream TXT_FILE (FULL_TXT_PATH, ofstream::out);

	// CHECK THE STATUS OF THE TXT FILE..
	// IF: TXT FILE NOT OK..
	if (!TXT_FILE)
	{
		cout << "\nError opening output file..\n";
		return;
	}
	// END IF..
	
	// IF: TXT FILE IS OK..
	if (TXT_FILE)
	{
		cout << "\nWrite was successful\n";
		FORMAT_TXT();
	}
	else cout << "\nWrite Failed..\n";
}
// END FUNCTION.. 


I've changed the folder name to "../MIDI_and_TXT_Files/" so its a bit more program / platform friendly..

But alas, its still not working. The error trap works and states "Error opening output file" but i just can' see why it won't work. It complies ok and no errors, just the file open / creation whatever part is not doing as intended. It's gonna be something obvious i know it haha..
You could try using the perror() function to see if you can get your system to help diagnose the problem.

http://www.cplusplus.com/reference/cstdio/perror/
Does the folder "MIDI_and_TXT_Files" already exist? If not, you should create it before running the program.

You also need to be aware that when using a relative path, first of all you need to be sure which is the starting location. This is the "working directory" of the program - it may be the same as the location of the executable program but need not be, especially if the program is run through an IDE.
Yeah i created the folder first. Its in the same folder as my .vcxproj and .cpp file. Is that the correct place?

If it's any help, i'm using visual studio 2015 and coding for windows.

Thanks jlb, i'll look into it :)
I just tested it with this:

string FULL_TXT_PATH = (TXT_NAME + ".txt");

...so not including a path and it works, saving it in the same directory as the .cpp file etc so that works but how do i get it to save in the other folder?
Do you need the "../" at the beginning of the path?
On a windows system, you can identify which is the current working directory like this. It might help you to understand what is happening.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

std::string workingdir()
{
    char buf[1024];
    
    GetCurrentDirectoryA(1024, buf);
    return buf;   
}


int main()
{
   
    cout << workingdir() << '\n';

}
That sorted it Chevril :) thank you. I knew it would be something obvious and / or simple.
Thanks everyone for the help..
Topic archived. No new replies allowed.