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

标签 c filestream

给定文件:

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/

相关文章:

c - 防止c中字符数组溢出

C - 程序无法正常工作,我错过了什么?

c - 使用 C - 如何单独打开目录中的每个文件,假设您的程序不在所述目录中

c# - 在 C# 中使用 FTP 上传带有编码的文件

c# - 如何正确地将CRLF(Environment.NewLine)添加到FileStream中?

c - 这个问题有什么算法方法吗?

c - 针对 libopcodes 编译时出错

c - 并行化 : bad results with threads, 进程的良好结果。为什么?

c# - "The process cannot access the file because it is being used by another process "

c# - 创建 FileTable 或 FileStream SQL 类型 在 C# 中使用 Stream