简体   繁体   English

读取文件并提取C中两行特定文本之间的行

[英]Read a file and extract lines between two lines of specific text in C

I am trying to extract text that is in between [HOCKEY] and [done with HOCKEY] from the following file: 我正在尝试从以下文件中提取[HOCKEY]和[HOCKEY完成]之间的文本:

sport.txt : sport.txt

[HOCKEY]
a=10
b=20
c=30
d=45
[done with HOCKEY]
[SOCCER]
a=35
b=99
c=123
d=145
[done with SOCCER]

With the following code I will be able to check if the line is [HOCKEY] but I am unable to record the location of 1st line and last line between [HOCKEY] and [done with HOCKEY] 使用以下代码,我将能够检查行是否为[HOCKEY],但无法记录[HOCKEY]和[done with HOCKEY]之间的第一行和最后一行的位置

#include<stdio.h>
int main()
{
FILE *infile;
char start, end;
char *sport="[]";
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
int line_number,i;
infile = fopen("sport.txt", "r");
printf("Opened file  for reading\n");
line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
    ++line_number;
    /* note that the newline is in the buffer */
    if (line_buffer=="[HOCKEY]")
    {
    start=line_buffer;
    printf("Found start %s",start);
  }
      if (line_buffer=="[done with HOCKEY]")
    end=line_buffer;

    while(start<end){
    printf("%c",start);
    start++;
    system("PAUSE");
    }
}
return 0;
}
  1. First line is the first line after [HOCKEY] . 第一行是[HOCKEY]之后的第一行。 And

  2. Last line is the last line before [done with HOCKEY] . 最后一行是[done with HOCKEY]之前的最后一行。

So what you need is to read the file one line by one line. 因此,您需要的是一行一行地读取文件。 When you read [HOCKEY] , you are approaching the real data you need and start to read and store the data from next line. 当您阅读[HOCKEY] ,您正在接近所需的真实数据,并开始从下一行读取和存储数据。 Continue this step until you read [done with HOCKEY] and stop. 继续此步骤,直到阅读[done with HOCKEY]并停止。

int count = 0;
char found_it = 0;
char *wordStart = "[HOCKEY]";
char *wordStop = "[done with HOCKEY]";
int charCount = strlen(wordStart);
while((c = getc(fptr)) != EOF){
    if( c == wordStart[count] ){
        count++;

        if(count == charCount){printf("Found [HOCKEY] \n"); found_it = 1; break;}
    }
    else{
        count = 0;
    }
}

if(!found_it){printf("Did not find [HOCKEY] \n"); return 0;}

count = 0; found_it = 0;
charCount = strlen(wordStop);
while((c = getc(fptr)) != EOF){
    printf("%c", c);
    if( c == wordStop[count] ){
        count++;

        if(count == charCount){found_it = 1; break;}
    }
    else{
        count = 0;
    }
}

if(!found_it){printf("Did not find [done with HOCKEY] \n");}
return 0;
#include <stdio.h>
#include <string.h>

int main(){
    FILE *infile;
    fpos_t start, end, pre_pos;
    char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
    //char line_buffer[128]; // BUFSIZ : I do not know if there is a large enough

    infile = fopen("sport.txt", "r");
    if(infile)
        printf("Opened file  for reading\n");
    else {
        perror("file open");
        return -1;
    }
    fgetpos(infile, &pre_pos);
    end = start = pre_pos;
    while (fgets(line_buffer, sizeof(line_buffer), infile)) {
        if (strcmp(line_buffer, "[HOCKEY]\n")==0){
            fgetpos(infile, &start);
        } else if (strncmp(line_buffer, "[done with HOCKEY]", 18)==0){//18 == strlen("[done with HOCKEY]");
            end=pre_pos;
            break;
        }
        fgetpos(infile, &pre_pos);
    }
    fsetpos(infile, &start);
    while(start<end){
        printf("%c", fgetc(infile));
        fgetpos(infile, &start);
    }
    fclose(infile);
    system("PAUSE");
    return 0;
}

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

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