8 Replies - 340 Views - Last Post: 23 June 2012 - 09:20 AM Rate Topic: -----

#1 porky101  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 97
  • Joined: 23-September 08

Question about sscanf()

Posted 23 June 2012 - 08:15 AM

#include <stdio.h>
#include <string.h>

void parse(char data[], char *command, char *key, char *val) {
  sscanf(data, "%s %s %s", command, key, val);
}

int main() {
  char buffer[1025], command[1025], key[1025], val[1025];

  printf("Enter command: \n");
  scanf("%s\n", buffer);

  parse(buffer, command, key, val);

  printf("%s\n", command);
  printf("%s\n", key);
  printf("%s\n", val);

   return 0;
}




Why does this not work? I am parsing a buffer into 3 strings, but the only one that gets copied over is the command, for the key and the val I just get some odd output instead of what it should be.

This post has been edited by porky101: 23 June 2012 - 08:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about sscanf()

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: Question about sscanf()

Posted 23 June 2012 - 08:27 AM

What input are you supplying to your program?

Also be careful when including constants in a scanf() type of function. You are probably having problems with this scanf() never ending:
scanf("%s\n", buffer);

The scanf() function usually does not pass the end of line character to your program so because you are requiring the end of line character to be entered you will have problems. Try removing the '\n'.

Your sscanf() in your function is also a problem. You will probably need to use strtok() instead of sscanf() to process your string.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 porky101  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 97
  • Joined: 23-September 08

Re: Question about sscanf()

Posted 23 June 2012 - 08:39 AM

View Postjimblumberg, on 23 June 2012 - 09:27 AM, said:

What input are you supplying to your program?

Also be careful when including constants in a scanf() type of function. You are probably having problems with this scanf() never ending:
scanf("%s\n", buffer);

The scanf() function usually does not pass the end of line character to your program so because you are requiring the end of line character to be entered you will have problems. Try removing the '\n'.

Your sscanf() in your function is also a problem. You will probably need to use strtok() instead of sscanf() to process your string.


Jim

I removed the \n and am still having the problem, why would my sscanf() be a problem, isn't that what it is supposed to do? The input is just three words with spaces in between

This post has been edited by porky101: 23 June 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: Question about sscanf()

Posted 23 June 2012 - 08:50 AM

Have you tried looking at the return value of your sscanf()? How many variables did it see? Does your input also have the commas? Show your actual input.

Have you actually printed out your "command" to insure you are actually inputting what you think you are? Remember scanf() stops processing when it encounters a space.


Jim

This post has been edited by jimblumberg: 23 June 2012 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 porky101  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 97
  • Joined: 23-September 08

Re: Question about sscanf()

Posted 23 June 2012 - 08:56 AM

I get a 1 output...there should be no commas, spaces instead, edited that in post


Ex:

Blah eat waffles


3 words with spaces
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: Question about sscanf()

Posted 23 June 2012 - 09:03 AM

Then since you are entering a string with spaces, using scanf() in main, you will only pass "Blah" to your function. To get a string that contains spaces you need to use fgets() instead of scanf() in main.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 porky101  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 97
  • Joined: 23-September 08

Re: Question about sscanf()

Posted 23 June 2012 - 09:07 AM

I don't undertstand, isn't fgets() for files? This is not file input
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: Question about sscanf()

Posted 23 June 2012 - 09:13 AM

Remember in C the console is a file, called stdin for input and stdout for output.
char test[100];
fgets(test,100, stdin);



Jim
Was This Post Helpful? 1
  • +
  • -

#9 porky101  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 97
  • Joined: 23-September 08

Re: Question about sscanf()

Posted 23 June 2012 - 09:20 AM

It woks thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1