gpt4 book ai didi

c - 如何在c中读取给定文件的一部分

转载 作者:行者123 更新时间:2023-11-30 21:25:58 24 4
gpt4 key购买 nike

给定文件:

test.txt

ac ab

ad am

av

al

ak aj an az ax

ag ah

我想从“am 到 an”读取,或者想在文件指针到达 am 时开始写入另一个文件,并继续写入,直到文件指针到达 az。这如何在 c 中完成。

最佳答案

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

#define WORD_SIZE 2

#define _S(x) #x
#define S(x) _S(x)

typedef enum { NO, YES } status;

int main(){
char word[WORD_SIZE + 1];
FILE *fin;
status Processing = NO;
fin = fopen("test.txt", "r");
while(1==fscanf(fin, "%" S(WORD_SIZE) "s", word)){
if(strcmp(word, "am")==0){
Processing = YES;
} else if(strcmp(word, "az")==0){
Processing = NO;
break;
}
if(Processing == YES){
fprintf(stdout, "%s\n", word);
}
}
fclose(fin);
return 0;
}

关于c - 如何在c中读取给定文件的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24140604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com