search file and put into struct

hi everybody,

I need some help with some programming.
I need to write a file that can search in a text file and read the whole line into a struct.

the struct =

struct Transistor 
{ 
  char chType[20];
  char chFabrikant[20];
  float fPrijs;
  enum Transistor_Behuizing { empty,TO18, TO39, TO126, TO66P,TO3P,TO3, X58, SOT23, SOT65, SOT323, SC75 } behuizing; 
};

and my file looks like this

BC107   fabriek   0.25  1
BC107A   fabriek   0.20  1
BC107B   fabriek   0.38  1
BC108   fabriek   0.15  1
BC108A   fabriek   0.85  1
BC108B   fabriek   0.94  1
BC108C   fabriek   0.24  1
BC109   fabriek   0.37  1
BC109A   fabriek   0.69  1
BC109B   fabriek   0.18  1
BC140   fabriek   0.73  2
BC140-6   fabriek   0.82  2
BC140-10   fabriek   0.25  2
BC140-16   fabriek   0.46  2
BC141   fabriek   0.39  2
BC141-6   fabriek   0.75  2
BC141-10   fabriek   0.20  2
BC141-16   fabriek   0.25  2
BC160   fabriek   0.36  2
BC160-6   fabriek   0.85  2
 

lets say I need to find the BC109 in the file, then I need to get a struct of

chType = BC109
chFabrikant = fabriek
fPrijs = 0.37
behuizing = 1

Can someone give me the right answer on how to do this, I've been searching for days now.

thanks if you help me

You'll never find the "right answer", since other people probably don't have the same problem as you word-for-word, letter-for-letter, keystroke-for-keystroke.

Try breaking it down into steps instead. You'll get more done, and in doing so, probably figure out where to go next.

1) read it line by line. fgets() does that.
2) Check for BC109 at the beginning of the line. strncmp can do that.
2) break it into tokens. Lots and lots of ways to do that.
3) Put it into the structure. One sscanf would do it in a quick-and-dirty fashion. A safer way would be splitting into tokens and copying or scanning individually.

So:

char buf[4096];
struct Transistor trans;

while(fgets(buf, 4096, stdin))
{
        int len=strlen("BC109");
        if(strncmp("BC109", buf, len) != 0) continue;
        // "BC109 ", not "BC109A"
        if(!isspace(buf[len])) continue;

        if(sscanf(buf, "%s %s %f %d", trans.chType, trans.chFabrikant, &trans.fPrijs, &trans.behuizing) != 4)
        {
                 fprintf(stderr, "couldn't scan line %s\n", buf);
                 exit(1);
        }

        fprintf(stderr, "Found BC109\n");
        break;
}
1 Like

I've been checking the program you wrote and it als picks out BC109A and BC109B :S

I only need it to pick out BC109

but if I search for BC109A, it only picks out BC109A

The code I gave you works.

These are the lines which match BC109 and reject BC109A:

        int len=strlen("BC109");
        // Do the first 5 characters match BC109?  If not, skip.
        if(strncmp("BC109", buf, len) != 0) continue;
        // It should find a space after BC109, not an A/B.  If it's not a space, skip.
        if(!isspace(buf[len])) continue;

Perhaps you changed one "BC109" but not the other? Try

        char *tofind="BC109";
        int len=strlen(tofind);
        if(strncmp(tofind, buf, len) != 0) continue;
        // "BC109 ", not "BC109A"
        if(!isspace(buf[len])) continue;

Or perhaps your data's slightly different than what you posted?

i founf out that i was not working with a space but a tab XD
but I found the solution.

rewind(fp);
do
{
   fgets(buf, 4096, fp);
   if(sscanf(buf, "%s%s%s%i", &transistor.chType, &transistor.chFabrikant,&chPrijs, &transistor.behuizing) != 4)
   {
       printf("couldn't scan line %s\n", buf);
   }
   if(strcmp(chTrans, transistor.chType) == 0)
   {
       printf("Found %s\n", transistor.chType);
   }
}while(!feof(fp));

But still many thanks, couldnt do it without your help.

My code works fine for tabs. That's why I used isspace(x) and not (x==' '), so it'd cover tabs or spaces. I don't know what you were running, but it wasn't my code.

That code works too, but has a very high probability of crashing whenever any of those fields are larger than your structure can hold.

the text in the file is coming from the same structure so thats no problem.

and I didnt mean to offend you sorry for that

Not offended, just puzzled. I don't know what code you were running, so I can't tell why it didn't work. Did you replace isspace()?

No I didn't replace isspace() at the first couple of times, then i went playing with the code and came with the the code I have now.