Issue with ofstream

I'm having some difficulty getting my program to write into a file using ofstream.
I checked my code but I can't find the error. Everything runs to completion but it doesn't write into the file.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

int main()
{
  ofstream file;
  file.open("file.txt");
  file << "TEST";
  file.close();

return 0;
}


All and any help is greatly appreciated!
Well I wasn't taught to do it that way, but this is how I would write the code:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

int main()
{
ofstream file("file.txt");
file<<"TEST";

return(0);
}
Still no file being written. I even copy&pasted it... I'm on a Mac using Xcode btw. Not sure if that would make a difference.
Sorry, I see what I did wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	
	ofstream file("File.txt");

	file<<"TEST"<<endl;

return(0);
}



I forgot about using namespace std; after the #include 's.

I think you could also do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

int main()
{
	
	std::ofstream file("File.txt");

	file<<"TEST"<<std::endl;

return(0);
}



Sooo, basically, you just forgot to use using namespace std; in your code...

Sorry that I didn't realize that in the first place!
Last edited on
okay so I tried using this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
std::ofstream file("file.txt");
file << "TEST";

return 0;
}

Still, nothing writes. I also tried using the code you provided, but that didn't work either. I wonder what's causing the issue...
Whoops, pasted the wrong code. Here is what I tested most recently:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream file("file.txt");
    file << "TEST";
    
return 0;
}
Works fine for me. Are you looking in the right place for your file?
This works for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>


using namespace std;

int main() {

	FILE *readtest = fopen ("readthis.txt", "w+"); //create text file for read/write.
	
	
	if ( readtest != NULL ) {
		fprintf ( readtest, "Hi" );
	}
	
	
	return 0;
}


EDIT
the w+ in this code creates the file and if such file is already created the w+ will delete and replace the old file with new one created. To only open a file and write to it use r+
Last edited on
Rather than just trying variations of the same code. you should check the status of the file and display a suitable message if a problem is found.
For example you can test if (!file.open()) before output, and if (!file.good()) afterwards.

But I suspect iHutch105 has hit the nail on the head, you may be looking in the wrong location (directory or subdirectory) for the file.
Last edited on
Thank you everyone for all your help!

It turns out that the issue was because I am on a Mac. Basically, I needed an absolute path, unlike windows where the file would be created where the .exe file is.

So instead of doing:
1
2
ofstream file("test.txt");
file << "test";


I needed to do:
1
2
3
ios_base::open_mode mode = ios_base::app;
file.open("/Users/Joe/Desktop/test.txt", mode);
file << "test";


Topic archived. No new replies allowed.