Writing to a txt file

Jan 5, 2017 at 5:41pm
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;
}

Jan 5, 2017 at 6:02pm
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
Jan 5, 2017 at 6:45pm
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.

Jan 5, 2017 at 6:59pm
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
Jan 5, 2017 at 7:52pm
@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.
Jan 5, 2017 at 8:46pm
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



Jan 6, 2017 at 3:51pm
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;
}
Jan 7, 2017 at 11:02am
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 Jan 7, 2017 at 11:07am
Jan 19, 2017 at 6:05pm
the file example.txt will be in your program source directory where your source file is
Last edited on Jan 19, 2017 at 6:06pm
Topic archived. No new replies allowed.