search file and put into struct


 
Thread Tools Search this Thread
Top Forums Programming search file and put into struct
# 1  
Old 12-08-2011
Error 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 =
Code:
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

Code:
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
Code:
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
# 2  
Old 12-08-2011
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:

Code:
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;
}

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-09-2011
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

Last edited by metal005; 12-09-2011 at 12:08 PM..
# 4  
Old 12-09-2011
The code I gave you works.

These are the lines which match BC109 and reject BC109A:

Code:
        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

Code:
        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?
# 5  
Old 12-09-2011
i founf out that i was not working with a space but a tab XD
but I found the solution.

Code:
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.
# 6  
Old 12-09-2011
Quote:
Originally Posted by metal005
i founf out that i was not working with a space but a tab XD
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.
# 7  
Old 12-09-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python Results Converted To C Struct Header File

I created python code that produce output in the form of: moses-red-sea=1.00.03 genesis-snake=2.03 deliverance=5.0.010 I need to take this output and create a "C" header file and have it look like this: struct { char *name; char *fixed_version; } filename_versions... (7 Replies)
Discussion started by: metallica1973
7 Replies

2. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

3. Programming

Can't assign struct variable in header file

Hi guys. I have a header file including a structure like this: typedef struct { int index = -1; stack_node *head; } stack; But when compiling with cc it shows error at the assignment line (int index = -1): error: expected ‘:', ‘,', ‘;', ‘}' or ‘__attribute__' before ‘=' token... (1 Reply)
Discussion started by: majid.merkava
1 Replies

4. Shell Programming and Scripting

How to search for multiple lines and put them into one paragraph?

Dear all, I'm trying to manipulate a data file and putting a certain lines into one paragraph. What am I actually want to do is that search some lines in a data file. These lines begin with "1\1\GINC-" and end with "\\@" or the following two empty lines as shown in blue. A part of the text... (11 Replies)
Discussion started by: liuzhencc
11 Replies

5. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

6. Shell Programming and Scripting

awk pattern search and put in a file

hi I am doing a task that is i have to search for multiple pattern from a file's 9 th column and then redirect it to a new file awk -F, 'BEGIN {OSF=","} { if ($9 == "OTF") print $0}' test1.tsv > test.tsv and the patterns are OST. ODI,MIN LIKE OTF Bit not getting the desired output... (11 Replies)
Discussion started by: pranabrana
11 Replies

7. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

8. Programming

writing binary/struct data to file

I am trying to write binary data to a file. My program below: #include <stdlib.h> #include <stdio.h> struct tinner { int j; int k; }; struct touter { int i; struct tinner *inner; }; int main() { struct touter data; data.i = 10; struct tinner... (4 Replies)
Discussion started by: radiatejava
4 Replies

9. Shell Programming and Scripting

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (2 Replies)
Discussion started by: shilendrajadon
2 Replies

10. UNIX for Advanced & Expert Users

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (1 Reply)
Discussion started by: shilendrajadon
1 Replies
Login or Register to Ask a Question