Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

simple sscanf

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
prady_21
Asked:
prady_21
  • 2
  • 2
  • 2
  • +3
1 Solution
 
akshayxxCommented:
hmm if some values are absent .. then u better read one by one and check after each scanf..
0
 
akshayxxCommented:
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
 
KocilCommented:
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
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!

 
KocilCommented:
oops ... correction

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

}
0
 
prady_21Author Commented:
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
 
chirilabogdanCommented:
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
 
chirilabogdanCommented:
correction

case 1: sscanf(p_word,"%s",p_word1);
0
 
gj62Commented:
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
 
dawg1Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now