Help with char strings

This is a discussion on Help with char strings within the C++ Programming forums, part of the General Programming Boards category; Ok... lets say that we have a char string that hold a directory, and its leght depends on user input ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    17

    Help with char strings

    Ok... lets say that we have a char string that hold a directory, and its leght depends on user input (C:\aaa, C:\bbbb, etc)

    Now i must add a "\*" in the end of that string, then replace this * with a subdirectory (C:\aaa\ccc) and then, add another "\*"

    I tried it with memcpy but i got compile errors, so i need suggestions/help

    Anynone avaible? thanks =]

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Without any code to see exactly what errors are these, the only suggestion is...

    Use std::string instead of char string
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I tried it with memcpy but i got compile errors, so i need suggestions/help
    *sigh* Did it cross your mind to post your code and the errors that are thrown when you try to compile it?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    The code is very big and confusing, because its not done
    but here is the code snippet

    Code:
      char * pch;
    pch=strrchr(DirSpec,'*');
    
    memmove (pch-DirSpec+1, arquivos[i].nome.c_str(), strlen(arquivos[i].nome.c_str()));
    and the compile error is

    Code:
    .\main.cpp(77) : error C2664: 'memmove' : cannot convert parameter 1 from 'int' to 'void *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    It's quiet confusing... You are using C++ strings mixed with char arrays. There is no need to. Simply use C++ strings. Forget memmove.

    Two strings and replace() will do it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    I know that is not a good idea mix both tipes, its because im working with some win32api functions: FindFirstFile, FindNextFile, FindClose

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Use strncpy() and strncat() then. Don't use the strcpy and strcat versions though. The 'n' versions, while not bulletproof, at least require for you to pass the size which is somewhat safer since it forces you to deal with it and better avoid out-of-bounds situations.

    Check their syntax and usage here: http://www.cppreference.com/stdstring/index.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    but those functions just add more chars, they cant remove the "*"

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    The third argument of strncpy will say how many characters to copy to the new string. Since the '*' is the last character...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can use C++ strings with FindFirstFile and friends. Just call c_str() like you did with memmove.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    Yes, i tought about it when i went to sleep. I'll try it right now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. help with strings
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 07:20 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21