简体   繁体   中英

Reading specific phrases from input file in C

Last question for the night. I try not to post more than once per struggle haha...

This one's a bit simpler.

I have a txt file with a series of arranged numbers in the first 8 lines. Every line after is a certain phrase like "BUY ITEM" or "AWARD ITEM" followed by an integer (there are several phrases but I'm only concerned with one). Basically I'm trying to have a for or while loop where I can detect the phrase in the document, set the pointer to the end of the phrase and then fscanf the integer to the right of the phrase. The only trouble I'm having is getting the pointer to the end of the specific phrase and then reading the number. As well as the fact that the phrase is repeated on different lines and I don't want the values to be taken all at once.

I'm sure I can do a simple

while (!feof(//function for reading phrase)) {
      fscanf("%d", &value);
      //rest of function

And that would be that. But I've tried fseek and fget and nothing has really been able to help get the pointer to the location I need it to without having a preset location of where to go. The input file will be different each time so I can't just tell it to go 1024 spaces down or something like that. Just not sure how you would even go about this...

Also below is an example of an input file.

75 75 908
10 10
18 23.10 10.09
70 5 15
8 100 20 28.99
30 40 50 60
4 6 8 8 5 5 5 6 7 10
10
BUY ITEM 8
BUY ITEM 10
AWARD ITEM 7
BUY ITEM 1
BUY ITEM 3
AWARD ITEM 9
BUY ITEM 7
RETURN ITEM 8

Much appreciation for anyone's help.

Here's an easy way to do it, using the fact that on the lines in question, if your file follows the same format then the numbers will always appear in the same character of the line. This is a little bit fragile, and it'd be better to make your program more robust, to cope with arbitrary amounts of whitespace, for instance, but I'll leave that as an exercise to you:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 100
#define BUY_LEN 9
#define AWARD_LEN 11

int main(void) {
    FILE * infile = fopen("file.dat", "r");
    if ( !infile ) {
        perror("couldn't open file");
        return EXIT_FAILURE;
    }

    char buffer[MAX_LEN];
    char * endptr;

    while ( fgets(buffer, MAX_LEN, infile) ) {
        if ( !strncmp(buffer, "BUY ITEM ", BUY_LEN ) ) {
            char * num_start = buffer + BUY_LEN;
            long item = strtol(num_start, &endptr, 0);

            if ( endptr == num_start ) {
                fprintf(stderr, "Badly formed input line: %s\n", buffer);
                return EXIT_FAILURE;
            }

            printf("Bought item %ld\n", item);
        }
        else if ( !strncmp(buffer, "AWARD ITEM ", AWARD_LEN) ) {
            char * num_start = buffer + AWARD_LEN;
            long item = strtol(num_start, &endptr, 0);

            if ( endptr == num_start ) {
                fprintf(stderr, "Badly formed input line: %s\n", buffer);
                return EXIT_FAILURE;
            }

            printf("Awarded item %ld\n", item);
        }
    }

    fclose(infile);
    return 0;
}

Running this with the sample data file in your question, you get:

paul@local:~/src/sandbox$ ./extr
Bought item 8
Bought item 10
Awarded item 7
Bought item 1
Bought item 3
Awarded item 9
Bought item 7
paul@local:~/src/sandbox$ 

Incidentally, based on one of the suggestions in your question, you might want to check out the answers to the question " while( !feof( file ) ) " is always wrong .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM