Substring to Char[]

This is a discussion on Substring to Char[] within the C++ Programming forums, part of the General Programming Boards category; Good Day Everyone, Once again I am having a problem, other that trying to wake up on a Monday. I ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    Substring to Char[]

    Good Day Everyone,

    Once again I am having a problem, other that trying to wake up on a Monday. I am trying to set a char[] = a substring. I keep getting a "cannot convert error". I get those quit often. Any suggestions on how I can learn to avoid this problem. More specifically, how can I correct this problem now?????

    Thanks,
    Kendal

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I am trying to set a char[] = a substring."

    Whatever that means.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You need to allocate some space there.

    I assume you are doing like:
    char a[] = something that returns a char *;

    a char[] is not a char*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    I not sure but this may work.

    strcpy(char,"substring");

    if i understand the question correctly

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    This is actually done in a class. I am actually reading a line at a time from a file into a string. I then want to pull fields from the string and store them into class members. Big Picture==Converting a 9 mb text file of patient records into a format that can be read into a MYSQL table. The text file has to be edited. Anyway, here is one of the lines of code:

    lname = readline->substr(8,19);

    lname is declared as "char lname[20]"
    readline is the string that holds the line from the file

    Any Ideas????

    Thanks,
    Kendal

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    lname[0] = my_string[0]
    etc.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Doesn't that only copy 1 character at a time????
    I just want to load the string returned by substr into a character array.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by gvector1
    lname = readline->substr(8,19);

    lname is declared as "char lname[20]"
    readline is the string that holds the line from the file
    you cannot do something like that. lname stores the address of 20 bytes of memory allocated for the array. the statement "name = readline->substr(8, 19);" attempts to change the address of lname to the value returned by substr().
    what you want to do is copy the string into the lname buffer:
    Code:
    std::string sub = readline->substr(8, 19);
    strncpy(lname, sub.c_str(), sub.size());
    lname[sub.size()] = NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split string and remove substring
    By nyc_680 in forum C Programming
    Replies: 3
    Last Post: 03-02-2009, 03:45 AM
  2. I need help with creating a substring program
    By CProgramingBegg in forum C Programming
    Replies: 9
    Last Post: 02-06-2009, 08:50 AM
  3. longest common substring problem
    By Enchanter88 in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2007, 11:02 AM
  4. Replacing a substring with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 10:37 AM
  5. Replies: 18
    Last Post: 06-21-2003, 10:57 AM

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