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!

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?

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!

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.

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.

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...

In windows, by including shlobj.h you can use a call to determine the path to the users desktop using the following:

// 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.

Ah, I see niek_e snuck in there with the answer while I was writing my last 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! ^_^

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?

but there's an error...
or just how can I open a file using your string path?

Tell us what the error is.

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! ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.