problem with sscanf in email client

L

lynx_ace

Hi everyone. I need a little bit help here...I have an assignment and
it is working fine until the last part which I can't solve. So here's
the code in simple form

#define maxlength 200


while( fgets( command, MAXLLENGTH, stdin ) != NULL )
{

if( sscanf( command, "%c %d%c%d", &c, &d ,e, &f)) {
switch( c ) {
case h = print the synopsis of all emails.
*if an integer is entered after h, then print the synopsis of
that message only
example h1 will print the synopsis of message 1 only
* if the input is h1-5 then it will print the synopsis
message 1 until 5

case p = print the real message (with the content ), and also
the case number such in 'h' is just the same

and so on which i have done perfectly (I guess so )....

the problem here is...

there should be a command 's', which will search a string entered in
the whole emails and printed the messages containing the string. That
means...after the command 's', we should put a string...
example: s true means it will search the string 'true' inside the inbox
and print all of them (if the subject contains true, or the contents
has a word 'true' )

However according to the sscanf provided above, I should put an integer
after the first command character. This is where I'm stuck at... and
yeah...I'm just learning C and the information about sscanf is too
little and rare in the internet. So I really appreciate if anyone can
give me a clue how to solve this problem. ^o^
 
J

Jack Klein

Hi everyone. I need a little bit help here...I have an assignment and
it is working fine until the last part which I can't solve. So here's
the code in simple form

#define maxlength 200


while( fgets( command, MAXLLENGTH, stdin ) != NULL )
{

if( sscanf( command, "%c %d%c%d", &c, &d ,e, &f)) {
switch( c ) {
case h = print the synopsis of all emails.
*if an integer is entered after h, then print the synopsis of
that message only
example h1 will print the synopsis of message 1 only
* if the input is h1-5 then it will print the synopsis
message 1 until 5

case p = print the real message (with the content ), and also
the case number such in 'h' is just the same

and so on which i have done perfectly (I guess so )....

the problem here is...

there should be a command 's', which will search a string entered in
the whole emails and printed the messages containing the string. That
means...after the command 's', we should put a string...
example: s true means it will search the string 'true' inside the inbox
and print all of them (if the subject contains true, or the contents
has a word 'true' )

However according to the sscanf provided above, I should put an integer
after the first command character. This is where I'm stuck at... and
yeah...I'm just learning C and the information about sscanf is too
little and rare in the internet. So I really appreciate if anyone can
give me a clue how to solve this problem. ^o^

The problem with the *scanf() series of functions is that you have to
provide the string with the conversion specifiers before you get the
data. So if you might have different data formats, you have to look
at the values of the initial objects that tell you the types of the
rest before you select or build a format string and call the *scanf()
function.

What you can do is something like this:

switch (command [0]
{
case 'h':
sscanf(command + 1, format_for_h, &var1, &var2, &var3);
/* do handling for 'h' */
break;

case 's':
sscanf(command + 1, format_for_s, &var4, &var5);
/* do handling for 's' */
break;
default;
/* do something with invalid command */
break;
}

That is, you need to look at the first character to determine how to
parse the rest of the line.

Even then, the *scanf() functions are not a good choice for
interactive user input, because they are rigid while users are not.
But if you must use sscanf() for your assignment, you need to know
which format string to call it with.
 
N

Nestea

switch (command [0]
{
case 'h':
sscanf(command + 1, format_for_h, &var1, &var2, &var3);
/* do handling for 'h' */
break;

case 's':
sscanf(command + 1, format_for_s, &var4, &var5);
/* do handling for 's' */
break;
default;
/* do something with invalid command */
break;

}
If I do this...Does that mean I have to use sscanf all way along for
each case??...and doesnt that mean if I got a lot of cases...I have to
use a lot of variables??....I wonder if it is possible if I do this...

sscanf ( command , " %c %s%c%s", &a, &b, &c ,&d )

and for cases that using integer as the input ( i.e h1, p1 ) I use the
library function atoi??...and for the 's' case, I just go along since I
got the string already in which I need.
 
C

CBFalconer

Nestea said:
switch (command [0]
{
case 'h':
sscanf(command + 1, format_for_h, &var1, &var2, &var3);
/* do handling for 'h' */
break;

case 's':
sscanf(command + 1, format_for_s, &var4, &var5);
/* do handling for 's' */
break;
default;
/* do something with invalid command */
break;

}
If I do this...Does that mean I have to use sscanf all way along for
each case??...and doesnt that mean if I got a lot of cases...I have to
use a lot of variables??....I wonder if it is possible if I do this...

sscanf ( command , " %c %s%c%s", &a, &b, &c ,&d )

and for cases that using integer as the input ( i.e h1, p1 ) I use the
library function atoi??...and for the 's' case, I just go along since I
got the string already in which I need.

Whatever you are trying to say, it is meaningless without context.
See my sig below for means of writing suitable articles with the
broken google usenet interface.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top