简体   繁体   English

从C中的输入文件读取特定短语

[英]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. 我有一个txt文件,在前8行中包含一系列排列的数字。 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). 之后的每一行都是一个特定的词组,例如“ BUY ITEM”或“ AWARD ITEM”,后跟一个整数(有几个词组,但我只关心一个)。 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. 基本上,我试图建立一个for或while循环,以便在其中可以检测文档中的短语,将指针设置为短语的末尾,然后将fscanf设置为短语右边的整数。 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. 但是我已经尝试了fseek和fget,但是没有预先设置要去的位置的方法,实际上没有什么能帮助您将指针指向我需要的位置。 The input file will be different each time so I can't just tell it to go 1024 spaces down or something like that. 每次输入文件都会不同,所以我不能仅仅告诉它向下移动1024个空格或类似的内容。 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 . 顺便说一句,基于问题中的一项建议,您可能想检查问题while( !feof( file ) )的答案总是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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