trouble with strings and characters

This is a discussion on trouble with strings and characters within the C Programming forums, part of the General Programming Boards category; Hello All, I am not new to this, just rusty... I am reading in a line of text from a ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    trouble with strings and characters

    Hello All,

    I am not new to this, just rusty...

    I am reading in a line of text from a file.

    I would like to parse through this line until I find the first ',' and take the characters before that and create a string:

    ie/ hello, world, how,

    I would like to end up with string1 = hello

    HOWEVER, unlike in C++ where I can just say: string1 = string1 + lineoftext[i],
    I am trying to use

    srtcat(string1, lineofText[i])

    My problem is its giving me a compiler error that i'm trying to use a character (lineoftext[i]) and not a string.

    any ideas would be greatly appreciated. thanks.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    srtcat(string1, &lineofText[i]);
    Kurt

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Or:
    Code:
    strcat(string1, lineofText + i);
    But it doesn't sound like you're trying to concatenate strings. It sounds like what you want is:
    Code:
    char ministring[BUFSIZ];
    char *ptr;
    
    ptr = strchr(lineofText, ',');
    strncpy(ministring, lineofText, ptr - lineofText);
    ministring[ptr - lineofText] = '\0';
    Last edited by itsme86; 08-02-2006 at 12:56 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    char getCampusId(char lineOfText[])
    {
    char campusId[8] ;
    int i=0;

    while(lineOfText[i] != ',')
    {
    printf("%c\n",lineOfText[i]);
    strcat(campusId, &lineOfText[i]);
    i++;
    }
    printf("Campus ID%s\n",campusId);
    return *campusId;
    }

    The lineOfText is: "1634792","Z01475820","01","Andrade"
    When I run this it prints:
    "
    segmentation fault

    What I would like to end up with is:
    "1634792"

    Not sure what the second suggestions was suggesting...thx again in advance

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Try the code I posted, see what you get.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    ahhhh...I think what I'm looking for is token, these are all words delimited by commas, so looks like I can pull them out with that, I'll be back if it doesn't work

    thanks for the replies!

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    FYI : for anyone else with this problem,

    The code below takes this line:

    "campusId","Adm_Id",DepartmentOwnerId

    And outputs this:

    Word: campusId
    Word: Adm_Id
    Word: DepartmentOwnerId


    char *token;

    if ((token = strtok(lineOfText, "\",")) != NULL )
    {
    do
    {
    printf("Word: %s\n", token);
    } while ((token = strtok(NULL, "\",")) != NULL);
    }

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Wow. Your original post didn't really explain what you needed.

    Maybe this would have worked better: What's the best way to chop up a string into smaller strings using a common seperator. (e.g. "string1,string2,string3" into the 3 strings "string1", "string2", and "string3")

    There was no mention in your question at all about wanting anything after the first seperator.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 08:53 AM
  2. Check strings which should only contain some characters.
    By qingxing2005 in forum C Programming
    Replies: 2
    Last Post: 06-17-2008, 09:32 AM
  3. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  4. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 10:28 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

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