Learn how to a build a cloud-first strategyRegister Now

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

question about sscanf

Hello experts,
Currently i have a method with code like this:
  sscanf(s, "%lf %lf %lf %lf %d %d ", &x, &y, &z, &weight, &se, &sl);   --- m1
this will read in the info about some 3D pionts from a file 'output'.
Now i am going to add one more property of the 3D points called 'name' and 'name' will be written to the 'output' file, so i changed the code to this:
   char name[20];
   sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, &name);  -- m2
The problem happens because not all 'output' files have 'name' saved. I want to add some checking: if there is a 'name', use m2 otherwise use m1.
Could anyone give me some idea how to add such a checking? How do i check 'name' exists?
Thanks.
0
lzha022
Asked:
lzha022
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi lzha022,

Use method 2.   If the first character of name is a new-line character or the end-of-string character, then no name was present on the line.



Good Luck!
Kent
0
 
dr_countryCommented:
If your file is a text file where you have 'name' as a stand alone label, you can search for it. For example, you can search the file line by line and if you find 'name', use your sscanf function, but that is if 'name' is in the file in its own line:

#include <stdio.h>

int searchString(FILE *fd, char name[25])
{
     char buffer[20];
     
     /* search through the file line by line.... */
     while ( (fgets(buffer, 20, fd)) != NULL )
     {
          if( strcmp(buffer,"name\n") == 0 )
               return 1;
     }
     return 0;
}
     

main()
{
     FILE *fd;
     char name[] = "name\n";
     char filename[] = "searchFile.txt";
     if ( (fd = fopen(filename, "r")) == NULL )
     {
          printf("Error %s\n", filename);
          return 1;
     }

     if( searchString(fd, name) )
        sscanf(s, "%lf %lf %lf %lf %d %d ", &x, &y, &z, &weight, &se, &sl);   \*--- m1  */
     else
        sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, &name); \*--- m2  */
   
     /* depending on your compiler you might not need the system() function */
     /*system("pause"); */
     fclose(fd);
     return 0;
}


0
 
PaulCaswellCommented:
Hi lzha022,

The trick to use here is to make use of the return value of scanf. I believe it returns the number of parameters matched or the number of values filled (there is a difference).

   int nScanned = sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, name);  -- m2 // Note that I have changed &name to 'name' as it is likely to be a char*.

Obviously you can now do

switch ( nScanned )
{
  case 6: // No name read.
...
  case 7: // Read a name from the file.
....


Paul
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
lzha022Author Commented:
Thanks all for your reply.
I forgot to mention, 'name' variable is entered by user so i have no way to know what it is.
Kdo`d method sounds good, i will try this first.
Thanks again
0
 
lzha022Author Commented:
Hi Paul,
I just read your comments. If this can work, it would be the easist way. I will try it now.

Thanks
0
 
lzha022Author Commented:
Thanks everyone. Paul`s method is really easy. Kdo`s method will also work i think though i have not tried it yet.
Thanks again.
Alison
0

Featured Post

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now