Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

OK this is a simple question but for some reason I just can't get it right. I have to scan from a file hundreds of lines of code and store it in a array (which I can normally do a ok job with) however At one point the code will specify a number that then corresponds to the next batch of chars ints and floats going into various arrays.

As I know I am not describing this correctly here is a example.

one line of the file I am reading will contain something close to this.

0221 T 2 S P 850 150 0.90 0.75 500 24 2 2012 G A 7 9600.00 0.1 1000 Name_of_place

0104 L 1 F 400 1.00 0.75 500 24 2 2012 G A 7 9600.00 0.1 1000 Ballroom

the problem I am having is This part here

0221 T 2 S P 850 150 0.90 0.75 500 24 2 2012 G A 7 9600.00 0.1 1000 Name_of_place

0104 L 1 F 400 1.00 0.75 500 24 2 2012 G A 7 9600.00 0.1 1000 Ballroom

The rest after this is Generally the exact same however at this point the number at the front descides all the values that are going in. I am almost completely lost on how to write a way that can scan this and store the data into arrays correctly

share|improve this question
    
This is a design question (I see no code here) so is perfectly on topic for us. –  ChrisF Mar 22 '12 at 22:11
    
Each field on your input must have a meaning that you should know about. If you care to share that, it will make understanding your problem easier. –  Emmad Kareem Mar 22 '12 at 22:39
1  
alright The entire thing is about venues. The first number is a code(dictating a place Like a P.O.box) then the second number is a type(T stands Theater) The bold number and the following are the parts I am confused about they deal with the Number of seating types the seating types the ammount of seats they hold how much they are expected to sell on a discount and how much they are expected to sell at normal price. the rest is hourly rate of venue. then the day month year of event type of event, size type. lengthin hours of event. fixed and precentage profit and name. –  Doc Mar 22 '12 at 22:43
1  
@Doc: can you tell us that in your question then? –  Dynamic Mar 22 '12 at 23:07
    
My problem is that I have to scan both lines of code and have it input correctly into the arrays. however the lines are not fixed and CANNOT be fixed so I have to have it where it uses fscanf and reads both of these lines and inputs the data correctly. –  Doc Mar 22 '12 at 23:49
add comment

2 Answers 2

One way to do it is to scan each line into an appropriately sized buffer using fgets:

fgets(buffer, BufferSize, fp);

and then use the string tokenizer:

char* pch = strtok(buffer, " ");
venue.code = atoi(pch);
pch = strtok(NULL, " ");
venue.type = pch[0];
pch = strtok(NULL, " ");
venue.numberOfSeatingTypes = atoi(pch);

Once you've grabbed the sizes of the variable bits, you can then malloc space for them:

venue.seatingTypes = (char *)malloc(sizeof(char) * venue.numberOfSeatingTypes);
venue.seatingSize = (int *)malloc(sizeof(int) * venue.numberOfSeatingTypes);

And then continue using the string tokenizer in a loop to get the variable seating types and size of each type.

Be aware that the string tokenizer is not re-entrant, so you may be better off using the re-entrant version.

share|improve this answer
    
re-entrant functions do not seem to be a requirement by the OP, nor would they seem to be needed for a valid solution to the problem. –  Greg A. Woods Nov 18 '12 at 23:04
1  
@GregA.Woods: Sure! But you're better off avoiding them. People tend to get into the habit of using the non-reentrant ones, and then wonder why the "same" solution fails in another case where reentrancy is important. –  Peter K. Nov 19 '12 at 0:30
add comment

Since it's line-oriented, I'd start by reading a line (e.g., with fgets).

Then I'd parse out the individual items in a line using sscanf. At least if I'm understanding correctly, you need to scan the first three (or so) fields, then based on the contents of (some of?) those, decide how to parse the rest of the line. That's, again, fairly straightforward. Just read in N fields (N=3, I think), then you'd do something like:

switch (venue_type) {
    case 'S':
        // read whatever
    case 'F'
        // read stuff for ballroom
    case 'T':
        // read stuff for theater
}

It may be easiest to re-read the line from the beginning for each type of venue, rather than trying to continue after the third (or so) field that you've already read.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.