943,578 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 752
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 24th, 2009
0

how to get a document from root?

Expand Post »
This is my next question: how to get a document from root?
because I have this code:
C++ Syntax (Toggle Plain Text)
  1. myfile.open ("/home/ws14/Desktop/test.txt");

But I need to change the path home/ws14 if I change user. Thanks!
Similar Threads
Reputation Points: 46
Solved Threads: 7
Junior Poster in Training
valtikz is offline Offline
51 posts
since Apr 2009
Apr 24th, 2009
0

Re: how to get a document from root?

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?
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: how to get a document from root?

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 46
Solved Threads: 7
Junior Poster in Training
valtikz is offline Offline
51 posts
since Apr 2009
Apr 24th, 2009
0

Re: how to get a document from root?

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.
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: how to get a document from root?

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
quartzie is offline Offline
1 posts
since Apr 2009
Apr 24th, 2009
0

Re: how to get a document from root?

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
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

Click to Expand / Collapse  Quote originally posted by valtikz ...
C++ Syntax (Toggle Plain Text)
  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 :

C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Apr 24th, 2009 at 5:54 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 24th, 2009
0

Re: how to get a document from root?

In windows, by including shlobj.h you can use a call to determine the path to the users desktop using the following:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Apr 24th, 2009
0

Re: how to get a document from root?

Ah, I see niek_e snuck in there with the answer while I was writing my last post!
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Apr 25th, 2009
0

Re: how to get a document from root?

Click to Expand / Collapse  Quote originally posted by quartzie ...
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! ^_^
Reputation Points: 46
Solved Threads: 7
Junior Poster in Training
valtikz is offline Offline
51 posts
since Apr 2009
Apr 26th, 2009
0

Re: how to get a document from root?

Click to Expand / Collapse  Quote originally posted by niek_e ...
Because it is Linux, so D:\ won't exist



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

C++ Syntax (Toggle Plain Text)
  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?

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 46
Solved Threads: 7
Junior Poster in Training
valtikz is offline Offline
51 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need some feedback on my coding
Next Thread in C++ Forum Timeline: Am I on the right track? (c++ code)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC