how to get a document from root?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 50
Reputation: valtikz is on a distinguished road 
Solved Threads: 7
valtikz's Avatar
valtikz valtikz is online now Online
Junior Poster in Training

how to get a document from root?

 
0
  #1
Apr 24th, 2009
This is my next question: how to get a document from root?
because I have this code:
  1. myfile.open ("/home/ws14/Desktop/test.txt");

But I need to change the path home/ws14 if I change user. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: how to get a document from root?

 
0
  #2
Apr 24th, 2009
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?
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: valtikz is on a distinguished road 
Solved Threads: 7
valtikz's Avatar
valtikz valtikz is online now Online
Junior Poster in Training

Re: how to get a document from root?

 
0
  #3
Apr 24th, 2009
  1. 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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: how to get a document from root?

 
0
  #4
Apr 24th, 2009
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.
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: quartzie is an unknown quantity at this point 
Solved Threads: 1
quartzie quartzie is offline Offline
Newbie Poster

Re: how to get a document from root?

 
0
  #5
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,963
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: how to get a document from root?

 
0
  #6
Apr 24th, 2009
Originally Posted by Dannyo329 View Post
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

Originally Posted by valtikz View Post
  1. 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 :

  1. #include<iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char text[255];
  8. FILE *name;
  9. name = popen("whoami", "r");
  10. fgets(text, sizeof(text), name);
  11. pclose(name);
  12. string path = "/home/" + string(text);
  13. cout << path << "\n";
  14. return 0;
  15. }

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...
Last edited by niek_e; Apr 24th, 2009 at 5:54 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 338
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 61
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: how to get a document from root?

 
0
  #7
Apr 24th, 2009
In windows, by including shlobj.h you can use a call to determine the path to the users desktop using the following:
  1. // This variable will store the path to the users desktop
  2. LPSTR desktopPath = new CHAR(MAX_PATH);
  3.  
  4. // this function call will populate the variable above with the path to
  5. // the users desktop....
  6. SHGetSpecialFolderPath(0, desktopPath, CSIDL_DESKTOPDIRECTORY, 0);

I've not done any programming for unix/linux (yet!), so I don't know anything about the various API's/libraries involved, but I'm sure there will be some equivalent functionality somewhere in the *nix codebase to do this. If anybody knows what it is, then that will be the most likely solution to this thread!

Hope this is in some way useful.
Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 338
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 61
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: how to get a document from root?

 
0
  #8
Apr 24th, 2009
Ah, I see niek_e snuck in there with the answer while I was writing my last post!
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: valtikz is on a distinguished road 
Solved Threads: 7
valtikz's Avatar
valtikz valtikz is online now Online
Junior Poster in Training

Re: how to get a document from root?

 
0
  #9
Apr 25th, 2009
Originally Posted by quartzie View Post
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! ^_^
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: valtikz is on a distinguished road 
Solved Threads: 7
valtikz's Avatar
valtikz valtikz is online now Online
Junior Poster in Training

Re: how to get a document from root?

 
0
  #10
Apr 26th, 2009
Originally Posted by niek_e View Post
Because it is Linux, so D:\ won't exist



You could get the username by using popen in combination with whoami :

  1. #include<iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char text[255];
  8. FILE *name;
  9. name = popen("whoami", "r");
  10. fgets(text, sizeof(text), name);
  11. pclose(name);
  12. string path = "/home/" + string(text);
  13. cout << path << "\n";
  14. return 0;
  15. }

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?

  1. #include<iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char text[255];
  8. FILE *name;
  9. name = popen("whoami", "r");
  10. fgets(text, sizeof(text), name);
  11. pclose(name);
  12. string path = "/home/" + string(text);
  13. cout << path << "\n";
  14.  
  15. return 0;
  16. }
I use this
  1.  
  2. fstream myfile;
  3. myfile.open (path + "/.test.txt", fstream::in | fstream::out | fstream::app);
  4. myfile.close();
but there's an error...
or just how can I open a file using your string path?
Last edited by valtikz; Apr 26th, 2009 at 8:29 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC