I want to create a txt file and print "hello" in the file. Im not sure what is wrong with my code, because the file does not show up in the documents folder after i run it.
Your code seems to be fine, couple of things I can think of:
(a) check the file path is correct and
(b) you need to compile and run the program, not just compile
because the file does not show up in the documents folder after i run it
Unless you're actually running the provided program from the documents folder, it won't place the file in the documents folder. This program will create the file in the program's current directory.
Unless you're actually running the provided program from the documents folder, it won't place the file in the documents folder.
I think this applies to command line but for IDE's it can differ, for e.g my C::B is in C:\ and my .txt files are (usually) in D:\. But yes, OP might be using command line
@fivestar, your file should be located at C:\directory\to project file\project_file_name\
as pointed by @gunnerfunner (if you are using an IDE like C::B or VS).
In other words, same folder that holds your .cpp file.
#include <iostream>
#include <fstream>
#include <stdlib.h>
usingnamespace std;
int main() {
ofstream myfile;
myfile.open("example.txt",ios::app); // use append how many times you compile your
// data not lost write in the next line of the exiting data
if(!myfile.fail())
{
myfile << "Hello" << endl;
}
else
{
cerr<<"Cannot Create File"<<endl;
cout<<"\nPress Any Key To Quit";
cin.get();
exit(1);
}
myfile.close();
system("pause");
return 0;
}