Saving to specific directory (GNU/Linux)

Feb 6, 2011 at 7:01pm
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 Feb 6, 2011 at 7:02pm
Feb 6, 2011 at 7:07pm
Simply specify full path in the name
eg:
 
ofstream somefile("~/Desktop/myfile");
Feb 7, 2011 at 8:04am
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
Feb 21, 2011 at 12:19pm
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
Feb 21, 2011 at 12:39pm
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.
Feb 21, 2011 at 12:48pm
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
Feb 21, 2011 at 1:20pm
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/
Feb 21, 2011 at 7:00pm
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
Feb 21, 2011 at 7:11pm
You are opening that file twice. Either pass the name in the constructor or call open.
Feb 21, 2011 at 7:21pm
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.