Thread: Help with string formatting problem...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    Help with string formatting problem...

    Ok so I've got a kind of weird question

    I need to take output such as:
    "12 Spiderman message goes here\n
    13 Superman message goes here"

    And reformat it like
    12: Spiderman - message goes here
    13: Superman - message goes here

    I tried using strtok but it ended up just being a mess. The whole problem with doing that is these statements (there could be anywhere from 1 - infinity lines) are all connected into one char array. I need an easier way to insert chars into these statements.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf + sprintf

    sscanf for %d, %s, %[^\n]
    sprintf %d space %s space %s

    Give it a shot and post your attempt / what you're stuck on specifically.


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

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by quzah View Post
    sscanf + sprintf

    sscanf for %d, %s, %[^\n]
    sprintf %d space %s space %s

    Give it a shot and post your attempt / what you're stuck on specifically.


    Quzah.
    That makes sense, but what exactly is the %[^\n] doing?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Here's what I just tried, but its giving me a segmentation fault. i have a feeling it might have to do with my misunderstanding of what %[^\n] is doing

    (response is the string)
    char str [100];
    char str2 [100];
    char strfinal[MAX_RESPONSE];
    int i;

    sscanf(response, "%d %s %[^\n]",i, str, str2);
    sprintf(strfinal, "%d: %s - %s", i, str, str2);
    printf("%s\n", strfinal);

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First, check the forum faqs for the use of code tags, it makes code a lot easier to read.

    Where is response defined?
    It's probably segfaulting because response is either undefined or too small.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    First, check the forum faqs for the use of code tags, it makes code a lot easier to read.

    Where is response defined?
    It's probably segfaulting because response is either undefined or too small.
    Ok I will check it out and that's not the case. Response is defined in the rest of my code. I've tested it to make sure response is ok. I can print off response fine with no segmentation error and it contains the string I posted in my initial comment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It may be in the rest of the code, but is it in a scope where the listed functions can see it?

    For example if you defined it inside main() it's possible with a function call outside of main, it isn't valid at that point in the program.

    Can you post your whole program... might make things easier, if it's not too long.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rkrajnov View Post
    That makes sense, but what exactly is the %[^\n] doing?
    It's stripping line feeds. If there is no chance of a trailing line feed you could try replacing that with %s which would read a second string.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    It may be in the rest of the code, but is it in a scope where the listed functions can see it?

    For example if you defined it inside main() it's possible with a function call outside of main, it isn't valid at that point in the program.
    No, it definitely is valid right there. It's defined just before this segment of code all in the very same method. I'm 99% sure the problem is in something I wrote out 2 posts ago. I just don't know what. And I still am not sure what %[^\n] is doing.
    Last edited by rkrajnov; 12-04-2010 at 11:14 PM.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    It's stripping line feeds. If there is no chance of a trailing line feed you could try replacing that with %s which would read a second string.
    What do you mean by stripping line feeds.... there definitely is a trailing line feed. Take a look at my initial post and you can see what the input is (note it could be 3, 4, 5+ lines as well).

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post a complete small version of your program. Not some huge crap we have to read through, but a small illustration of the issue. Go read the format specifier sections for scanf/printf to better understand what it's doing. (here)


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

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rkrajnov View Post
    What do you mean by stripping line feeds.... there definitely is a trailing line feed. Take a look at my initial post and you can see what the input is (note it could be 3, 4, 5+ lines as well).
    Ok... there are linefeeds in the file... but are they stripped when you load it into the string?

    Quzah is right... we need to see your code before we can do much more...

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Here's the main relevant method. It gets a string response through a program given to us. The sendCommand method outputs into response (so response has right output no questions asked). Then I removed the '\r' (not really important) and alter the "response". That works fine and is irrelevant to my question pretty much.

    Code:
    void get_messages() {
    	char response[ MAX_RESPONSE ] = "";
    	char lastNum[10];
    	sprintf(lastNum, "%d", lastMessage);
    	sendCommand(host, port, "GET-MESSAGES", user, password, lastNum, response);
    	
    	char *temp;
    	int x = 0;
    	temp = response;
    	
    	//removes /r
    	while(x < strlen(response)-2) {
    		if (response[x] != '\r')
    			*temp++ = response[x];
    		if (response[x] == '\r')
    			lastMessage++;
    		x++;
    	}
    	*temp = 0;
    	char str [100];
    	char str2 [100];
    	char strfinal[MAX_RESPONSE]= "";
    	int i;
    	
    	messages = malloc(MAX_RESPONSE * sizeof(char));
    	
    	if(strcmp(response, "NO-NEW-MESSAGES") != 0){
    		sscanf(response, "%d %s %[^\n]",i, str, str2);	
    		sprintf(messages, "%d: %s -  %s", i, str, str2);
    	}
    	else{
    		messages = "";
    	}
    }
    messages is printed down here after initial call of get_messages and then through every loop: (messages is global variable)
    Code:
    void * getMessagesThread(void * arg) {
    	// This code will be executed simultaneously with main()
    	// Get messages to get last message number. Discard the initial Messages
    	char response[ MAX_RESPONSE ] = "";
    	get_messages(); //the method posted above
    	while (1) {
    		// Get messages after last message number received.
    		get_messages();
    		// Print messages
    		printf("%s", messages);
    		// Sleep for ten seconds
    		usleep(2*1000*1000);
    	}
    }
    Now If I replace the sscanf and sprintf with messages = strdup(response), messages prints perfectly. keep that in mind.
    Last edited by rkrajnov; 12-04-2010 at 11:37 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, that answered my question... response does not have carriage returns in it.

    Try it like this...
    Code:
    	if(strcmp(response, "NO-NEW-MESSAGES") != 0){
    		sscanf(response, "%d %s %s",i, str, str2);	
    		sprintf(messages, "%d: %s -  %s", i, str, str2);

  15. #15
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    Ok, that answered my question... response does not have carriage returns in it.

    Try it like this...
    Code:
    	if(strcmp(response, "NO-NEW-MESSAGES") != 0){
    		sscanf(response, "%d %s %s",i, str, str2);	
    		sprintf(messages, "%d: %s -  %s", i, str, str2);
    unfortunately I tried that and it results in a segmentation fault as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM