Writing to a txt file

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>


using namespace std;



int main() {

	ofstream myfile;
	myfile.open("example.txt");
	myfile << "Hello" << endl;
	myfile.close();
	

	system("pause");
	return 0;
}

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.
On a Windows system, use the function GetCurrentDirectory()


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

using namespace std;

void showDir()
{
    char buff[MAX_PATH];
    int n = GetCurrentDirectoryA(MAX_PATH, buff);
    
    if (n)
        cout << "Current Directory is:\n" << buff << '\n';
    else
        cout << "Get Directory failed\n";
}

int main() 
{
    showDir();
    
    ofstream myfile;
    myfile.open("example.txt");
    myfile << "Hello" << endl;
    myfile.close();
    

    system("pause");
    return 0;
}


https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx



try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;

int main() {

	ofstream myfile ("example.txt");
        if (myfile.is_open()){
	myfile << "Hello" << endl;
        }
        else if(!myfile.is_open()){
        cout << "Sorry file is not opened";
	}
        myfile.close();

	return 0;
}
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
#include <iostream>
#include <fstream>
#include  <stdlib.h>


using namespace 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;
}
Last edited on
the file example.txt will be in your program source directory where your source file is
Last edited on
Topic archived. No new replies allowed.