How to save files under the same directory?

Hi there,

I am having problems understanding how to save files under the same directory for i/o programs. Can someone please explain to me how to save files under the same directory? Thanks!

If you want to save it under the same directory simply give it a file name instead of a directory and file name.

1
2
3
std::ofstream out("file.txt"); //will save in current directory

out << "Hello, world!";
I'm sorry, I'm still confused.
I am working on a program that requires to to open a text file and display it when I run my program. I used notepad on my windows laptop to write up the necessary information I needed for the text file. How can I make my program open up that text file?
The directory part is still confusing.
Sorry for the bother!
If it is is the same directory as the program you can simply type the filename. Otherwise you will have to give a full path. To open you will need ifstream instead of ofstream.

1
2
3
4
5
6
7
std::ifstream in("file.txt");

std::string input;
while(std::getline(in, input))
{
    std::cout << input << std::endl;
}


http://www.cplusplus.com/reference/fstream/
http://www.cplusplus.com/reference/fstream/ifstream/
http://www.cplusplus.com/reference/fstream/ofstream/
http://www.cplusplus.com/reference/fstream/fstream/
Topic archived. No new replies allowed.