C语言 逐字读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16400886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:15:52  来源:igfitidea点击:

Reading from a file word by word

cfilescanf

提问by Andrea Gottardi

I have a custom archive structured as follows:

我有一个自定义存档结构如下:

%list% name1 name2 name3 %list%

%dirs% archive directories %dirs%

%content% name1 path1 content of file1 %content%
%content% name2 path2 content of file2 %content%
%content% name3 path3 content of file3 %content%

%list%contains names of files in archive
%dirs%contains names of directories
%content%lists file contents.

%list%包含存档中的文件名
%dirs%包含目录
%content%列出文件内容。

Since i need to print the content of a specified file, i want to read this archive word by word, in order to identify %content%tag and file name.
I know the existence of fscanf(), but it seems to work efficiently only if you know the archive pattern.

由于我需要打印指定文件的内容,我想逐字读取这个存档,以便识别%content%标签和文件名。
我知道 的存在fscanf(),但它似乎只有在您知道存档模式时才能有效地工作。

Is there a C library or command, like ifstreamfor C++, that allows me to read word by word?

是否有 C 库或命令,例如ifstreamC++,允许我逐字阅读?

Thanks

谢谢

回答by jxh

You can just use fscanfto read one word at a time:

你可以fscanf一次只读一个单词:

void read_words (FILE *f) {
    char x[1024];
    /* assumes no word exceeds length of 1023 */
    while (fscanf(f, " %1023s", x) == 1) {
        puts(x);
    }
}

If you don't know the maximum length of each word, you can use something similar to this answerto get the complete line, then use sscanfinstead, using a buffer as large as the one created to read in the complete line. Or, you could use strtokto slice the read in line up into words.

如果您不知道每个单词的最大长度,您可以使用类似于此答案的方法来获取完整行,然后使用sscanf与创建的缓冲区一样大的缓冲区来读取完整行。或者,您可以使用strtok将读取的内容分成单词。

回答by BLUEPIXY

like this

像这样

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

typedef char Type;

typedef struct vector {
    size_t size;
    size_t capacity;
    Type *array;
} Vector;

Vector *vec_make(){
    Vector *v;
    v = (Vector*)malloc(sizeof(Vector));
    if(v){
        v->size = 0;
        v->capacity=16;
        v->array=(Type*)realloc(NULL, sizeof(Type)*(v->capacity += 16));
    }
    return v;
}

void vec_add(Vector *v, Type value){
    v->array[v->size] = value;
    if(++v->size == v->capacity){
        v->array=(Type*)realloc(v->array, sizeof(Type)*(v->capacity += 16));
        if(!v->array){
            perror("memory not enough");
            exit(-1);
        }
    }
}

void vec_reset(Vector *v){
    v->size = 0;
}

size_t vec_size(Vector *v){
    return v->size;
}

Type *vec_getArray(Vector *v){
    return v->array;
}

void vec_free(Vector *v){
    free(v->array);
    free(v);
}

char *fin(FILE *fp){
    static Vector *v = NULL;
    int ch;

    if(v == NULL) v = vec_make();
    vec_reset(v);
    while(EOF!=(ch=fgetc(fp))){
        if(isspace(ch)) continue;//skip space character
        while(!isspace(ch)){
            vec_add(v, ch);
            if(EOF == (ch = fgetc(fp)))break;
        }
        vec_add(v, '##代码##');
        break;
    }
    if(vec_size(v) != 0) return vec_getArray(v);
    vec_free(v);
    v = NULL;
    return NULL;
}

int main(void){
    FILE *fp = stdin;
    char *wordp;
    while(NULL!=(wordp=fin(fp))){
        printf("%s\n", wordp);
    }
    return 0;
}