Thread: mydrive + mydir

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    90

    mydrive + mydir

    Hello,
    I hope that so basic questions are allowed.

    In VisualBasic we write:
    mydrive = "C:\"
    mydir="Data\"
    myFolder = mydrive + mydir

    1) How to join mydrive and mydir to get myFolder in C++?
    2) Did C++ have any handy funcion to translate windows slashes "\" with unix type slashes "/"?clearance
    Last edited by nime; 03-27-2010 at 11:05 AM. Reason: clearance

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    I forget to note that in my case both mydrive and mydir are declared as const char * and i cannot get to work mydrive + mydir.
    Additionally, I would like to know how to determine on which OS (win or linux) program run.
    Thanks in advance.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You type
    Code:
    myFolder = mydrive + mydir;
    to join the two together (assuming you've declared everything of type string). You can use / in paths in Windows (from C, not from the command line).

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    string mydrive = "C:\"
    string mydir="Data\"
    string myFolder = mydrive + mydir
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Thank you tabstop.
    This is wery useful if I can use "/" in win and solves my problem.

    MK27,
    I am very new in c++ and I have declared variables as const char*.
    Is here a simple way to join those two const chars like with "+".

    And last, I asked for some "IsWindows" or "IsLinux" function in C++.
    Did this exists, where is and how to use.

    Thanks.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your problem is that you have declared your string variables as const char*. Don't do that. (There is a conversion for string variables for use to functions that need const char * for hysterical reasons, like your ofstream things. This conversion is called c_str.)

    Generally, every system defines some symbol that you can test with #ifdef (WIN32 on Windows, and who-knows-what depending on what flavor of Linux you've got).

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well when writing cross-platform code you could stick something like this at the top of your file:
    Code:
    bool bIsWindows = false;
    #ifdef _WIN32
    bIsWindows = true;
    #endif
    ..
    // IsWindows()
    bool IsWindows()
    {
         return bIsWindows;
    }
    
    // IsLinux
    bool IsLinux()
    {
         return !bIsWindows;
    }
    Note you may need to confirm the actual #ifdef value, I am doing it from memory and have not used it in over a year...

    As for the const char * and math (+) no you cannot since you are in this case adding two pointers together, not appending strings. You have two choices:
    1. If you insist on using const char * call strcat(myFolder, myDrive); strcat(myFolder, myDir);
    2. Use string objects as MK suggested above. Pretty sure everyone is going to suggest you do the latter...
    Last edited by jeffcobb; 03-27-2010 at 12:38 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nime View Post
    I am very new in c++ and I have declared variables as const char*.
    Is here a simple way to join those two const chars like with "+".
    No, you would need to use a C-string function, as long as the destination string is long enough to hold more. However, in any case a const variable should not/cannot be modified after initialization. So:
    Code:
    #include <cstring>
    const char *mydrive = "C:\"
    const char *mydir="Data\"
    char myFolder[strlen(mydrive)+strlen(mydir)+1]
    sprintf(myFolder, "%s%s",mydrive,mydir);
    The first two do not need a declared length because they will be considered C-string literals. MyFolder must be declared big enough to hold both of them.

    Using a C++ string is easier in this case; as tabstop says if you need a const char parameter use:
    Code:
    string my Folder;
    [...]
    ifstream(myFolder.c_str(),...)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Thanks wery much for all useful advices.
    Now I have to try all of this.

    I commes from VisualBasic and there we uses just strings for such things so I have missunderstanding of pointers.
    OK for this purpose it is simpler to use strings in C++.
    But (maybe I'm wrong) it seems that routines with char* is faster than those with strings what can be important when we have to replace some chars in million of strings.
    I am working on that

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nime View Post
    But (maybe I'm wrong) it seems that routines with char* is faster than those with strings what can be important when we have to replace some chars in million of strings.
    I am working on that
    By faster you mean:
    1) execution speed, or
    2) ease of implementation, ie, writing the coding

    WRT #1, I dunno. Could be.

    WRT #2, it's more the case that there are some C string functions and methods that are not perfectly duplicated with C++ string methods, but you can still use them on C++ strings, eg:
    Code:
    	string str = "hello world";
    	char *p = strchr(str.c_str(),'w');
            p[3] = 'X';
            cout << p;
    If you don't want to #include <cstring>, you could do this:
    Code:
    	string str = "hello world";
            string::size_type pos = str.find_first_of("w");
            str[pos+3] = X;
            cout << str.substr(pos,str.size()-pos);
    which is certainly more awkward and long winded and probably does execute slower.

    WRT coming from Visual Basic, two big things you will want to understand:
    1) the difference between dynamic typing (VB) and static typing (C/C++)
    2) the difference between dynamic memory allocation (VB and C++ strings) and static memory allocation (C strings).

    The significance of the first one is, in C/C++, you must declare variables to be of a specific type and you cannot change them. Coming from VB, you probably have not had to think much about datatypes because VB uses dynamic typing.

    The significance of the second one is that C arrays have a fixed sized.
    Code:
    char mystring[20];
    mystring can only hold 20 characters, because it is an array. If you want to make it hold more, you need to declare it as a pointer, malloc() it memory, and then realloc() it later.

    Also, in C/C++ you also must often take responsibility for "deallocating" or "freeing" memory when you are done using it. Coming from VB, you will not have needed to think about this as it is all automatic.

    The major reason for these two differences is that VB is an interpreted language whereas C/C++ is compiled. No one really wants to put up with static typing or manual allocation, but up to now it has been impossible to create a compiled language with completely dynamic typing and allocation, so you have to deal with it.

    I just said you cannot change a variable type in C/C++, but you can cast it. This is the case where you want to submit a C-string to a function that requires a C++ string:
    Code:
    void some_function(string str);
    // to call with a C-string:
    char mystring[] = "hello world";
    some_function((string)mystring);
    This is like how you can submit a C++ string as a C string using .c_str().

    I imagine all this is maybe bewildering right now, but don't worry, it will all make sense eventually...
    Last edited by MK27; 03-27-2010 at 02:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    90
    Thank you MK27.
    Huh, complicated for now as I can see.
    I decide to go step by step how I can accept and I belive it will go.
    Well, VB is also nice language. Easy to use, fast in execution and programming but unfortunatelly MS did not make quality sucessor for them so I move off MS influence to avoid situation like this in the future.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nime View Post
    Well, VB is also nice language. Easy to use, fast in execution and programming but unfortunatelly MS did not make quality sucessor for them so I move off MS influence to avoid situation like this in the future.
    Interpreted languages are great (I actually don't use VB, but I love perl, which is also interpreted). You will find doing "the same thing" in C++ requires a lot more code.

    However, there are advantages, such as "the same thing" in C++ will execute way faster. Also, I found learning a compiled language improved my understanding of what I was doing, mostly because of the explicit need to handle datatypes and memory*. Like I said, keep those two concepts in the your mind -- datatypes and memory; memory as in bytes. A char is one byte (8 bits), so a char array is a series of bytes in memory.

    * both the VB interpreter and the perl interpreter are actually programs written in C!
    Last edited by MK27; 03-27-2010 at 03:00 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The major reason for [...] have to deal with it.
    You will find doing "the same thing" in C++ requires a lot more code.
    However [...] such as "the same thing" in C++ will execute way faster.
    O_o

    Don't know where to begin...

    Soma

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    O_o
    Don't know where to begin...
    Soma
    Is that a first?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed