new c users question

This is a discussion on new c users question within the C Programming forums, part of the General Programming Boards category; I just started learning c recently, and im trying to put it to 'good' use by writing a small program ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    new c users question

    I just started learning c recently, and im trying to put it to 'good' use by writing a small program that can take a string of words and numbers and sort them into a struct array, heres what Ive come up with so far:

    Code:
    #include <stdio.h>
    
    typedef struct   
    {
                    char PlayerClass[20];
    	char PlayerRace[10];
    	int PlayerLevel;
    
    }RpgStructData;  
    
    RpgStructData StructTester;   
    
    
    main()
    {
    
    	char *delimiter; 
    	char delTester[] ="Shadow Knight,Dark Elf,15";
    	delimiter = strtok(delTester, ",");
    
    	while (delimiter != NULL)
    	{	
    		printf("%s", delimiter);
    		delimiter = strtok(NULL, ",");
    	}
    
    
    strcpy(RPGStructTester.PlayerClass);
    printf("players class is: %s\n", RPGStructTester.PlayerClass); 
    
    
    }
    The delimitor section in the main essentially breaks the string up into seperate tokens (I think thats what theyre called?). The names might be a bit odd but its not for a MUD or anything inparticular, I just like rpgs

    Anyway my problem is after the deliminator has broken the string into seperate tokens (comma is used as the delimitor)., I cant figure out how to pass the values of each token into the struct Rpgstruct.
    I know I need to use strcpy to copy each individual token into the corresponding struct data entry, but I just cant figure it out, and couldnt find any online guides on something so specific.
    I guess I just really dont know the syntax etc of c yet.

    But if anyone could give me a hand in the right direction I'd really appreciate it

    tim
    Last edited by timS; 10-05-2004 at 06:37 PM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    hello, internet!

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Better edit your post and put that code in [ CODE ][ /CODE ] brackets (w/o the spaces) before Salem catches you.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    woops sorry I thought the tags were for saving the formatting. still, I wont make the mistake again. so any clues?

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Could you post the code that doesn't work where you are doing the string copy? Perhaps with that we can offer better advice...
    Last edited by pablo615; 10-05-2004 at 06:04 PM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    ok, bearing in mind I know this doesnt work. Ive been a bit hard pressed just working out how to do it:

    Code:
    strcpy(RPGStructTester.PlayerClass);
    printf("players class is: %s\n", RPGStructTester.PlayerClass);
    I cant work out how to copy the the individual tokens of the string to the corresponding data types specified under the struct , ie 'Shadow Knight' to PlayerClass, '15' to PlayerLevel etc

    I can see the strcpy help file under my post, Im just not certain what the tokens are called, how do I identify each one?

    Does this help understanding what Im trying to do at all? Sorry I know these are kind of low level questions
    Last edited by timS; 10-05-2004 at 06:16 PM.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    The man page for strcpy:

    Code:
    NAME
           strcpy, strncpy - copy a string
    
    SYNOPSIS
           #include <string.h>
    
           char *strcpy(char *dest, const char *src);
    
           char *strncpy(char *dest, const char *src, size_t n);
    
    DESCRIPTION
           The  strcpy()  function  copies the string pointed to by src (including
           the terminating `\0' character) to the array pointed to by  dest.   The
           strings  may not overlap, and the destination string dest must be large
           enough to receive the copy.
    
           The strncpy() function is similar, except that not more than n bytes of
           src  are copied. Thus, if there is no null byte among the first n bytes
           of src, the result will not be null-terminated.
    
           In the case where the length of src is less than that of n, the remain-
           der of dest will be padded with nulls.
    
    RETURN VALUE
           The  strcpy()  and strncpy() functions return a pointer to the destina-
           tion string dest.
    
    BUGS
           If the destination string of a strcpy() is not large enough  (that  is,
           if  the programmer was stupid/lazy, and failed to check the size before
           copying) then anything might happen.  Overflowing fixed length  strings
           is a favourite cracker technique.
    You need both source and destination when using strcpy()

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    You are also going to need to save each token in the loop, not outside of it.

    Plus, if you look closely, you are going to have a buffer overflow....bad stuff.

    PK

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    ah your right, will fix that bit

    Using the delimiter loop from my original code, something like this is what I interpreted could be possible:

    Code:
    while (delimiter != NULL)
    	{	
    		printf("%s", delimiter);
    		delimiter = strtok(NULL, ",");
                                    strcpy(RpgStructData.PlayerClass, delimiter);
    	}
    I think I that strcpy(unitTester.UnitCode, delimiter); wont work as that past part is supposed to be a string? the delimitor references each token part of the string though, how can I pass its this way reference into the struct?

    Let me know if im completely off here, my compiler wont parse this so its been real guess work

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by timS
    I think I that strcpy(unitTester.UnitCode, delimiter); wont work as that past part is supposed to be a string? the delimitor references each token part of the string though, how can I pass its this way reference into the struct?



    Another thing to think about is that assigning each delimter to the right field is difficult in a loop. And don't forget that the last token, 15, will be a string, and your structure defines an int for level, so you will need to convert.

    Loops are useful if you are doing the same work repeatedly...here you are actually doing something different for each token (assigning the token to a different attribute of the structure). You can either take the effort to write it so you can iterate (a tall task for a beginner) or skip the loop for tokenizing the string.

    PK

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use sscanf?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    have been attempting to change my code, but ive got to get off to bed just now. will be back tomorrow though

    -tim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  3. Finding users on a network and messaging them.
    By Necrodeemer in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2003, 03:04 PM
  4. Windows ntfs perms
    By wp_x in forum Tech Board
    Replies: 3
    Last Post: 03-04-2003, 06:38 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

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