Saving to specific directory (GNU/Linux)

Hi all!

I just read the tutorial on input/output with files (http://www.cplusplus.com/doc/tutorial/files/).

I think I understand most of what I am reading, but what I don't understand is how to tell a program where to save a file. Let's say I want it to be saved in my ”Desktop” directory, normally I would point to:

~/Desktop

I have done a search on Google, but not found anything that clarifies this.

Thanks for reading this,

HMW
Last edited on
Simply specify full path in the name
eg:
 
ofstream somefile("~/Desktop/myfile");
Bazzy, hi!

Simply specify full path in the name


For once, I am pleased that an operation is actually easier than I thought it would be!

Thank you for your reply, I appreciate it.

Best,

HMW
Ok. I finally got a chance to try this, but I am apparently not doing it right. Here is a simplified example of what I am trying to do:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  string name;
  int year;
  ofstream testfile("~/Desktop/testfile.txt");

  cout << "Write your name: ";
  getline(cin, name);

  cout << "How old are you? ";
  cin  >> year;

  testfile.open ("testfile.txt"); // I have tried specifying the full
// pathname here as well - no dice!

  testfile << "Your name is: "
          << name << ".\n"
          << "You are "
          << year << " years.\n";

  testfile.close();

  return 0;
}


It works if I do not try to specify the full pathname, as such:

ofstream testfile;

Then the file is saved in the current directory, and the output is as expected. I'm sure I'm doing a really stupid mistake, nevertheless - I would appreciate if someone could point it out for me.

Thanks in advance,

HMW
ofstream testfile("~/Desktop/testfile.txt");
http://www.cplusplus.com/reference/iostream/ofstream/ofstream/
the stream is associated with a physical file as if a call to the member function open with the same parameters was made.

http://www.cplusplus.com/reference/iostream/ofstream/open/
If the object already has a file associated (open), the function fails.
Thanks, I appreciate it, but I don't get it. Any chance you could point out in the code above what is wrong, and how it should be in order for it to work?

Best,

HMW
Maybe "~" doesn't expand to your actual home so the file won't open.
You gen get the actual home directory from the environment ( getenv("HOME") ) and then append the relative path
eg: ( string(getenv("HOME"))+"/Desktop/testfile.txt" ).c_str()
http://www.cplusplus.com/reference/clibrary/cstdlib/getenv/
Bazzy, hi!

Ok, yes, you were right. I found out what was wrong. I needed to use an ABSOLUTE path in my code, like this:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  string name;
  int year;
  ofstream testfile("/home/HMW/Desktop/testfile.txt"); // Absolute path

  cout << "Write your name: ";
  getline(cin, name);

  cout << "How old are you? ";
  cin  >> year;

  testfile.open ("testfile.txt");

  testfile << "Your name is: "
          << name << ".\n"
          << "You are "
          << year << " years.\n";

  testfile.close();

  return 0;
}


Now, the file ends up where I want it - but it's empty!

I guess this has something to do with what ne555 posted, but for the life of me I cannot understand the error!

Best,

HMW
You are opening that file twice. Either pass the name in the constructor or call open.
Ok, now i got it. Thanks for your patience. Here is the working version:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  string name;
  int year;
  ofstream testfile; // Changed here...

  cout << "Write your name: ";
  getline(cin, name);

  cout << "How old are you? ";
  cin  >> year;

  testfile.open ("/home/HMW/Desktop/testfile.txt"); // and here.

  testfile << "Your name is: "
          << name << ".\n"
          << "You are "
          << year << " years.\n";

  testfile.close();

  return 0;
}
Topic archived. No new replies allowed.