| | |
how to get a document from root?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
This is my next question: how to get a document from root?
because I have this code:
But I need to change the path home/ws14 if I change user. Thanks!
because I have this code:
C++ Syntax (Toggle Plain Text)
myfile.open ("/home/ws14/Desktop/test.txt");
But I need to change the path home/ws14 if I change user. Thanks!
C++ Syntax (Toggle Plain Text)
myfile.open ("/home/ws14/Desktop/test.txt");
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 1
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.
•
•
•
•
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.

•
•
•
•
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!C++ Syntax (Toggle Plain Text)
myfile.open ("/home/ws14/Desktop/test.txt");
C++ Syntax (Toggle Plain Text)
#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...
Last edited by niek_e; Apr 24th, 2009 at 5:54 am.
In windows, by including shlobj.h you can use a call to determine the path to the users desktop using the following:
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.
C++ Syntax (Toggle Plain Text)
// This variable will store the path to the users desktop LPSTR desktopPath = new CHAR(MAX_PATH); // this function call will populate the variable above with the path to // the users desktop.... 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!
Those who understand binary .....
And those who don't!
•
•
•
•
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.
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! ^_^
•
•
•
•
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)
#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...
But how can I write a file using the string path?
C++ Syntax (Toggle Plain Text)
#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; }
C++ Syntax (Toggle Plain Text)
fstream myfile; myfile.open (path + "/.test.txt", fstream::in | fstream::out | fstream::app); myfile.close();
or just how can I open a file using your string path?
Last edited by valtikz; Apr 26th, 2009 at 8:29 pm.
![]() |
Similar Threads
- doc root in windows virtual hosting (PHP)
- Forbidden | You don't have permission to access /~username on this server. (Linux Servers and Apache)
- Repeat text from somewhere else in a document without actually writing it! (PHP)
- Document Root paths and circles (Search Engine Optimization)
- httpd setup problem (*nix Software)
- parsing error (XML file-java-schema) (Java)
- domain/virtual hosts all messed up, can't see the files in FTP (Linux Servers and Apache)
- Include a file from another website (ASP)
Other Threads in the C++ Forum
- Previous Thread: need some feedback on my coding
- Next Thread: Am I on the right track? (c++ code)
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference rpg simple string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






