Hello1! First af all, excuse me for my bad english!

I need to read user input that contains a certain command. My problem is that it depends on the command, the amount of strings i'll need to read. For example, there's a command with the following sintaxis:

Command1 name = value
and there are others like
Command2 name

and so on...
Between the name of the command and the following text always a space occurs... in the other fields, there can be zero to any number of spaces, so a command like
Command1 name = 45 is valid as well as Command1 name=45


I'm having a lot of troubles with this.. i tried a lot of things but none of it worked.. i tried to google and learn how to user scanf properly but i just dont get it... so if you can help me, i'll appreciate an example and its explanations. I dont know if scanf can be use with variable amounts of variables, but thats not the main problem cause anyway i can scanf multiple times by checking the name of the command first. What i really dont understand is how ignore spaces.. how to ignore, for example, spaces, plus newline characters plus, for example, the "=" symbol...

Well.. excuse me again, i dont know if someone will understand this but i had to try!! Thanks in advance!!

Unless the input is *strictly* formatted, don't use scanf(). You want a char buff[90] (nice and big) to hold a full line of input from the user or file, *PLUS* one newline: '\n' char and one end of string marker char: '\0'.

The format for it is:

char buff[90]={'\0'};
fgets(buff, sizeof(buff), stdin);

Where stdin could be the name of any input stream (like a file pointer even).

Now you'll take in a whole line up to the first <enter> key (which makes the newline, and thus the end of the line).

Now you can work your way through the buff array with sscanf() or whatever you want, as many times as you want, or need to.

Try that, and post up your problems, and we'll jump in to assist as needed. Trust me, it's THE way that you want to take in input from a user, when it's not going to be strictly formatted. (Which is most of the time, in R/L). ;)

And welcome to the forum! ;)

Edited 6 Years Ago by Adak: n/a

hank you Adak!!

I understand what you say, i work a few examples and then use the strtok function to split (i think the term is "split" for dividing the string) and it worked great!! But it's for a course and they wont let us use that function, dont ask me why but they only want scanf instead.... a friend just told me.. not even strtok.

The thing is that they're going to prove the program with what you called strictly formated input... the only thing we need to worry is to ignore the spaces between the sintax of the commands...

I read somewhere that something like this

scanf("%s[^\n]%*c", commandName, c);

would read the command name, and the newline or blank space will be read in the variable c, so the scanf that comes after it wont read it at the begin...

But for some reason when the program execute the second scanf:
scanf("%[^,=\n]%*c", nomVar, c)

nomVar gets the name right but with a space at the first place!! So.. i dont know how to fix it

And thanks for the welcome!! As i'm new in C i'll surely be around here with more questions haha!! So be patient!!

This article has been dead for over six months. Start a new discussion instead.