how to get a document from root?
This is my next question: how to get a document from root?
because I have this code:
myfile.open ("/home/ws14/Desktop/test.txt");
But I need to change the path home/ws14 if I change user. Thanks!
valtikz
Junior Poster in Training
52 posts since Apr 2009
Reputation Points: 46
Solved Threads: 7
I don't really get what you are asking, do you mean you have the test.txt file on the desktop and the code for opening it is for a user, but you want to open the file again on another user?
Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
myfile.open ("/home/ws14/Desktop/test.txt");
for example: /home/ws14 is my user. but if I transfer the c++ file to another user it cant open the path /home/ws14. because it is not the same user. how do I make the path general? with a folder. that you dont need to change the code for the user and another user. thanks!
valtikz
Junior Poster in Training
52 posts since Apr 2009
Reputation Points: 46
Solved Threads: 7
Why don't you just put the text file in your hard drive like D:\ or something? Then the program will work on every user on the computer.
Dannyo329
Junior Poster in Training
79 posts since Apr 2008
Reputation Points: 20
Solved Threads: 8
Why don't you just put the text file in your hard drive like D:\ or something? Then the program will work on every user on the computer.
Because it is Linux, so D:\ won't exist ;)
myfile.open ("/home/ws14/Desktop/test.txt");
for example: /home/ws14 is my user. but if I transfer the c++ file to another user it cant open the path /home/ws14. because it is not the same user. how do I make the path general? with a folder. that you dont need to change the code for the user and another user. thanks!
You could get the username by using popen in combination with whoami :
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
pclose(name);
string path = "/home/" + string(text);
cout << path << "\n";
return 0;
}
I didn't test it, but I think it should work. There's probably a better/cleaner way to do it. Perhaps use "~" in the filename?
[edit] too late...
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
on a u*x system, / is your root. /home/ws14 is a private home directory, so you probably need to place the text file into some public directory instead. if you don't care on whose desktop the file is, you can try accessing ~/Desktop/text.txt, but it will be up to the other users to ensure they have such file on their respective desktop.
Yes this is a good help quartzie. But is there a posible code to auto detect the user? or a code that can create a folder on public and inside the folder is the textfile that you can read/write?.
because if you just put it in Desktop it has a posibility to delete that file even if it is hiden file .text.txt. and then the C++ code for the read is being crashed.
To ALL Thanks for the help! ^_^
valtikz
Junior Poster in Training
52 posts since Apr 2009
Reputation Points: 46
Solved Threads: 7
Because it is Linux, so D:\ won't exist ;)
You could get the username by using popen in combination with whoami :
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
pclose(name);
string path = "/home/" + string(text);
cout << path << "\n";
return 0;
}
I didn't test it, but I think it should work. There's probably a better/cleaner way to do it. Perhaps use "~" in the filename?
[edit] too late...
This is a good help! Thanks!
But how can I write a file using the string path?
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
pclose(name);
string path = "/home/" + string(text);
cout << path << "\n";
return 0;
}
I use this
fstream myfile;
myfile.open (path + "/.test.txt", fstream::in | fstream::out | fstream::app);
myfile.close();
but there's an error...
or just how can I open a file using your string path?
valtikz
Junior Poster in Training
52 posts since Apr 2009
Reputation Points: 46
Solved Threads: 7
but there's an error...
or just how can I open a file using your string path?
Tell us what the error is.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Tell us what the error is.
Thanks to all! I already get it. I use this:
FILE *name;
name = popen("whoami", "r");
char text[(sizeof(name) + 1)];
fgets(text, sizeof(text), name);
pclose(name);
string string_text = text;
string path = "/home/" + string_text;
string testpath = path + "/.test/.test.txt";
char * test_path;
test_path = new char[(testpath.size() + 1)];
strcpy(test_path, testpath.c_str());
ofstream myfile;
myfile.open (test_path);
myfile << "TEST";
myfile.close();
My fault is I dont translate a string to char. Because the myfile.open doesn't detect a string value.
this is my code for that :
string testpath = path + "/.test/.test.txt";
char * test_path;
test_path = new char[(testpath.size() + 1)];
strcpy(test_path, testpath.c_str());
Thanks again! hope to help me again if I have some problems! ^_^
valtikz
Junior Poster in Training
52 posts since Apr 2009
Reputation Points: 46
Solved Threads: 7