简体   繁体   English

在 C 中逐字读取文本文件

[英]Reading a text file word by word in C

I am trying to learn file I/O operations in C.我正在尝试学习 C 中的文件 I/O 操作。 My aim is storing all the words to a string array from a text file.我的目标是将所有单词从文本文件存储到字符串数组中。

I tried to write a code that reads text file char by char and creates words from those chars.我试图编写一个代码,逐个字符地读取文本文件并从这些字符中创建单词。 What I want to do is accessing to a word with line and word index.我想要做的是访问带有行和单词索引的单词。 Would you help me about this?你能帮我解决这个问题吗?

This is my code.这是我的代码。

char words[MAX_LINE][MAX_WORDS][MAX_CHAR];
int i=1, j=1, k=1;

FILE* ptrbook;
if((ptrbook = fopen("trial.txt", "r")) == NULL)
{
    printf("Failed to open the file %s\n", filename);
}
else
{
    while((words[i][j][k] = getc(ptrbook)) != EOF)
    {
        while((words[i][j][k] = getc(ptrbook)) != '\n')
        {
            while((words[i][j][k] = getc(ptrbook)) != ' ')
            {
                words[i][j][k] = getc(ptrbook);
                k++;
            }
            if((words[i][j][k] = getc(ptrbook)) == ' ' )
            {
                j++;
                k=1;
            }
            if((words[i][j][k] = getc(ptrbook)) == '\n')
            {
                i++;
                j=1;
                k=1;
            }
        }
    }
}
printf("%s\n", words[1][1]);

For doing this issue is easiest reading all the content from file into a dynamic variable in the heap.对于这个问题,最简单的方法是将文件中的所有内容读取到堆中的动态变量中。 So after define a variable use the below function to allocate the dimension for the variable.因此,在定义变量后,使用下面的 function 为变量分配维度。

#define SIZE 4096
char * malloc_buff(int dim){
    char *buf;
    buf=malloc(sizeof(char)*dim);
    if(buf==NULL){
        perror("Errore in malloc");
    }
    return buf;
}

Then you perform a variable and store all the information of the file in that variable using the below function.然后执行一个变量,并使用下面的 function 将文件的所有信息存储在该变量中。 Instead of database_prenotazioni.txt insert the name of your file name而不是 database_prenotazioni.txt 插入文件名的名称

char * lettura_database_prenotazioni(){
    FILE* fd;
    char* disponibili;
    disponibili=malloc_buff(SIZE);
    errno=0;
    fd=fopen("database_prenotazioni.txt","r+");
    if(errno!=0){
        fprintf(stderr,"error in open\n");
        exit(-1);
    }
    fread(disponibili,sizeof(char),SIZE,fd);
    if(fclose(fd)){
        fprintf(stderr,"error in closure\n");
        exit(-1);
    }
    return disponibili;
}

Now you need to make a strtok of the variable in which you have all the content with the del \t.现在您需要使用 del \t 对包含所有内容的变量创建一个 strtok。 Remember to allocate a new variable as array_stringhe variable in the below function that should be used for the return of the function请记住在下面的 function 中分配一个新变量作为 array_stringhe 变量,该变量应该用于 function 的返回

char ** tokenize_elem(char *buffer,char *del){
    int c=1;
    char **array_stringhe=malloc((sizeof(char*)*SIZE));
    if(array_stringhe==NULL){
        fprintf(stderr,"errore in malloc\n");
        exit(EXIT_FAILURE);
    }

    char *token=malloc_buff(SIZE);
    memset(token,'\0',SIZE);
    memset(array_stringhe,'\0',SIZE);
    array_stringhe[0] = strtok (buffer,del);
    //fprintf(stdout,"%s\n",array_stringhe[0]);
    while(token!=NULL){
        token=strtok(NULL,del);
        if(token==NULL){
            break;
        }
        array_stringhe[c]=token;

        //fprintf(stdout,"%s\n",array_stringhe[c]);
        c++;

    }
    free(token);
    return array_stringhe;
}

Try to call getc(ptrbook) only once per loop and store the result temporarly.尝试在每个循环中仅调用一次 getc(ptrbook) 并临时存储结果。 Than you can check for the splitting characters such as white space and line breaks.您可以检查分割字符,例如空格和换行符。 Currently you are jumping in each while loop from character to character, which is not what you want here.目前,您正在从一个字符跳到另一个字符的每个 while 循环,这不是您想要的。

Do something like this:做这样的事情:

int c;
while((c = getc(ptrbook)) != EOF){
    if(c == ' ') 
        j++;
    else if(c == '\n') 
        i++;
    else 
        words[i][j][k++] = c;
}

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

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