Find the answer to your Linux question:
Results 1 to 3 of 3
I have three question: 1. How to replace quotation mark when I want to use for file path, for example in fout("/home/somebody/ file .txt") I wanted to change the file ...
  1. #1
    Linux Enthusiast minthaka's Avatar
    Join Date
    May 2006
    Location
    Mol, Vojvodina
    Posts
    556

    C++ questions

    I have three question:
    1. How to replace quotation mark when I want to use for file path, for example in fout("/home/somebody/file.txt") I wanted to change the file path using the other file name typed in by the user ? I dont want to use path explicitly, but instead dinamically changing paths.
    2. How to declare a string constant:
    I use
    const char home[15]="/home/minthaka/";
    but there's an error.
    3. Do I have to use strcat(s1,s2) to concatenate strings or could I have
    s3= s2+s3;
    If you need a CD/DVD catalogizer, give a try to my program:
    http://www.kde-apps.org/content/show...content=100682
    Linux Usert#430188

  2. #2
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    Quote Originally Posted by minthaka View Post
    I have three question:
    1. How to replace quotation mark when I want to use for file path, for example in fout("/home/somebody/file.txt") I wanted to change the file path using the other file name typed in by the user ? I dont want to use path explicitly, but instead dinamically changing paths.
    2. How to declare a string constant:
    I use
    const char home[15]="/home/minthaka/";
    but there's an error.
    3. Do I have to use strcat(s1,s2) to concatenate strings or could I have
    s3= s2+s3;
    you can use sprintf to change file path.
    sprintf(PATH,"/home/somebody/%s",filneme);

    and for string constant
    char *const home[15]="/home/minthaka/";

    try to check these

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    1) If you're asking how to dynamically generate a string, sprintf() is indeed the way to go. It takes in a buffer, a pattern (the same style as printf()), and the values to fill into the pattern. It's basically like using printf() except that it stores the value into the provided buffer instead of printing it. See "man sprintf" for more details.

    If that's not the question, please be clearer.

    2) What error are you getting? That code compiles fine for me.

    3) You have to use strcat(). The reason for this is simple. Let's assume we have the following code:
    Code:
    ...
    char *s1 = "Hello ";
    char *s2 = "World";
    ...
    So here, s1 is a character pointer, and s2 is a character pointer. Because they are pointers, their only value is actually a single number that is the memory address that they reference. If you attempt to add them together (s1 + s2), you would actually be doing pointer arithmetic, which would figure out the memory location exactly (value of s2) 1-byte (or whatever the size of a char is) chunks down the line.

    strcat(), on the other hand, looks at the actual values pointed to by the pointers, and works with those chars (and not the pointers to chars).


    If you don't know about pointers yet, then don't worry: a pointer is basically a variable that references a value, rather than actually being that value itself. You will learn that arrays are basically pointers.


    I hope that helps! Let us know if you have any questions.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...