Like Tree1Likes
  • 1 Post By AndrewHunter

Reading from config file and attempting to use strtok() to end of line

This is a discussion on Reading from config file and attempting to use strtok() to end of line within the C Programming forums, part of the General Programming Boards category; Basically, here's the function in question: Code: static void ConfPassSwhois(aClient *aptr) { char *myport = NULL, *mypass, *mywhois = NULL; ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Question Reading from config file and attempting to use strtok() to end of line

    Basically, here's the function in question:
    Code:
    static void ConfPassSwhois(aClient *aptr) {
    	char *myport = NULL, *mypass, *mywhois = NULL;
    	ConfigItem_except *e;
    	char line[512];
    	int len = 0;
    	for (e = portpass; e; e = (ConfigItem_except *) e->next) {
    		strcpy(line,e->mask);
    		myport = strtok(line, ":");
    		if (myport) {
    			mypass = strtok(NULL, ":");
    			mywhois = strtok(NULL, ":");
    			len = strlen(mywhois);
    			if ((atoi(myport) == aptr->listener->port)) {
    				if (mywhois && len < 35)
    					strcpy(swhois, mywhois);
    				else
    					strcpy(swhois, "SWHOIS info is too long!!");
    			}
    		}
    	}
    }
    The variable swhois has been previously defined in the program as 'static char *swhois[50]; '.

    The line in question is "mywhois = strtok(NULL, ":");" as it's reading in 18 characters from the file properly but doesn't continue reading to the end of the line. It is intended to load something like this example '80:somepassword:some description with spaces' from the file and assign it to variables. Everything works (with numerous warnings) except that at the end of it all, 'myswhois' only contains 18 (of say 30) characters. Is there a way i can make strtok() read in more characters? If not, what should I use instead to read the line in?

    Thanks for any [positive] feedback.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > The variable swhois has been previously defined in the program as 'static char *swhois[50]; '.
    But this is an array of pointers to char, so minimally, the syntax would be

    strcpy(swhois[someindex], mywhois);

    But it is just an array of pointers at present. You also need to allocate memory for each pointer as well.

    swhois[someindex] = malloc( strlen(mywhois) + 1 );
    strcpy(swhois[someindex], mywhois);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Thanks for your response. If i changed it from an array to just static char *swhois; Would that make any difference? Also, the line variable 'mywhois' only contains 19 characters (instead of 18 in the original post, sorry) when reading in via strtok(). What insight could you share on that? It is, so far, copying to 'swhois' perfectly fine.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Currently you have 50 fingers, pointing to nothingness (that is, you have pointers but nothing for them to point to). If you change it to just a static char*, then you still don't have any memory, but just one finger pointing to nothingness. If you believe you are supposed to have a static buffer of memory, then you want
    Code:
    static char swhois[50];
    Last edited by tabstop; 08-04-2011 at 04:22 PM. Reason: counting

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by chevyman2002 View Post
    Thanks for your response. If i changed it from an array to just static char *swhois; Would that make any difference? It is, so far, copying to 'swhois' perfectly fine.
    However it is not copying to swhois fine. char* swhois is just a pointer. You need to actually allocate space to hold anything in it, as Salem already said. What you are seeing is undefined behavior. The easiest way to do this is just make swhois a char array, e.g. char swhois[50];

    You need to revisit strtok(). Read this. I would suggest you make a simple program and get the strtok part working first, then include it into your actual program.
    beatbox32 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    I appreciate everyone's input on this matter, it has helped out greatly. Evidentally, there was another function that broke the buffer up and allocated memory into seperate variables before the function I wrote is called. It wasn't copying the entire buffer to the array in the other function and that's why I was missing information.. Again, thanks to everyone who responded, I appreciate your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2011, 03:45 AM
  2. Replies: 7
    Last Post: 12-13-2010, 01:13 PM
  3. C linux - reading config file.
    By Fujitaka in forum C Programming
    Replies: 5
    Last Post: 11-30-2010, 10:10 AM
  4. Reading file with strtok
    By creilly in forum C Programming
    Replies: 6
    Last Post: 11-09-2010, 12:15 AM
  5. reading file and strtok
    By Butters007 in forum C Programming
    Replies: 7
    Last Post: 12-02-2006, 02:15 AM

Tags for this Thread


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