?
Solved

simple sscanf

Posted on 2003-03-11
9
Medium Priority
?
416 Views
Last Modified: 2012-06-27
Basically i am trying to run a program like
cat file | ./cprogram.c
I read in the lines of the file using gets.
The line is of the form( seperated by tabs)
word word word word
The no of words are a max of 5.
What i would like to do is to assign these values to specific variables, which i am doing using sscanf, but
when one of the values is absent, the wrong values will be assigned to the variables.
sscanf(line,"%s\t%s\t%s\t%s\t%s", str1, str2, str3, str4, str5);
Any help appreciated.
Thanks
0
Comment
Question by:prady_21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117473
hmm if some values are absent .. then u better read one by one and check after each scanf..
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8117491
the thing is .. if u have one value missing ..then if u read all the values at one sscanf .. then u can tell that one is missing .. but u CANNOT tell which one .. cause it always will be the last word..
so fix a format of the file .. and stick to it ..
and if error then nuthing much u can do .. but to report the error .. and exit OR continue with some default values
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8117723
Better use strtok for this.

main()
{
    char word[5];
    char buffer[80];
    char *token;



    gets(buffer);
    token = strtok(buffer, " \t");
    for (i=0; i<5 && token != NULL; i++) {
       strcpy(word[i], token);
       token = strtok(NULL, " \t");
    }
    while(i<5) word[i++] = 0;

    ...
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Kocil
ID: 8117731
oops ... correction

main ()
{
   char word[5][10];
   char buffer[80];
   ...

}
0
 
LVL 3

Author Comment

by:prady_21
ID: 8118013
Well, the problem with strtok is that the identity of the string is lost, and therefore i cannot use it.
Anyway thanks for trying
Any more help gladly accepted
0
 

Expert Comment

by:chirilabogdan
ID: 8118879
Hello,

I know that this is not a fine solution, but:
try to adapt your input file to something like this:
2 word word
1 word
3 word word word
5 word word word word word
basically add a int equal to number of words that you have.

#define MAX_LINE_LEN 128

char p_word[MAX_LINE_LEN];
char p_word1[MAX_LINE_LEN], p_word2[MAX_LINE_LEN];
char p_word3[MAX_LINE_LEN], p_word4[MAX_LINE_LEN];
char p_word5[MAX_LINE_LEN];

To read a entire line you have to use:
fgets(p_word, MAX_LINE_LEN, file);

p_word[0] represents your int, in first case 2

then you can do something like this:
switch(p_word[0]) {
    case 1: sscanf(p_word,"%s %s",p_word1);
    break;
    case 2: sscanf(p_word,"%s %s",p_word1, p_word2);
    break;
    case 3: sscanf(p_word,"%s %s %s",p_word1, p_word2, p_word3);
    break;
    case 4: sscanf(p_word,"%s %s %s %s",p_word1, p_word2, p_word3, p_word4);
    break;
    case 5: sscanf(p_word,"%s %s %s %s %s", p_word1, p_word2, p_word3, p_word4, p_word5);
    break;
    default :
    error;
}


Good luck!!!



0
 

Expert Comment

by:chirilabogdan
ID: 8118887
correction

case 1: sscanf(p_word,"%s",p_word1);
0
 
LVL 6

Expert Comment

by:gj62
ID: 8119825
What do you mean, "the identity of the string is lost"?

If you mean that strtok modifies the string, simply make a copy of it first before parsing...

0
 

Accepted Solution

by:
dawg1 earned 100 total points
ID: 8128946
Combine the ideas already mentioned:
  1) Copy the string to a temp buffer
  2) Use strtok to determine how many words there are
  3) Use a switch to select the correct sscanf

Or consider something like:

main() {
    char *str_ptr, word[5][10];
    int count, i;

    str_ptr = "one\ttwo\tthree\tfour\tfive";
    count = get_words(str_ptr, word);
    printf("There are %d words:\n",count);
    for (i=0;i<count;i++) {
       printf("   %s\n",word[i]);
    }
}

/*Routine to count words and put them into an array */
int get_words(char *string, char ptr[][10])
{  
   char buffer[80];
   char *token;
   int i;

   strcpy(buffer,string);
   token = strtok(buffer, " \t");
   for (i=0; i<5 && token != NULL; i++) {
      strcpy(ptr[i], token);
      token = strtok(NULL, " \t");
   }
   return(i);
}

Note: I borrowed heavily from previous suggestions...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question