为什么strtok用\ n,只读取txt文件的第一行?

时间:2022-04-19 19:25:34

I have had some problems with strtok, I am trying, in my code in C, read from a text file, however, only the fist line is being read, because for some reason, the variable arq becomes NULL after first line.

我在使用strtok时遇到了一些问题,我正在尝试,在我的C代码中,从文本文件中读取,但是,只读取第一行,因为由于某种原因,变量arq在第一行后变为NULL。

the txt:

ola
oi
a
b
e
z

code:

void le_arquivo(char *optarg) {
    FILE *respostas;                
    char *arq;                      
    char resp[SBUFF];              
    char *tokens[SBUFF];           
    int c = 0;

    respostas = fopen(optarg, "r");
    if (respostas == NULL) {
        printf("Erro ao abrir o arquivo\n");
        return;
    }
    fgets(resp, sizeof(resp), respostas);
    arq = strtok(resp, "\n");      
    while (arq != NULL) {
        tokens[c] = arq;
        arq = strtok(NULL, "\n");
        c++;
    }
    fclose(respostas);
    organiza_dados(tokens, c);
} 

1 个解决方案

#1


3  

strtok does not read from the file, it splits the character array. By definition fgets() reads a single line including the trailing newline if present in the file, so strtok() only splits a single item. Instead of fgets(), you should try and read the whole file with fread(), and null terminate the array before the strtok() loop:

strtok不读取文件,它会分割字符数组。根据定义,fgets()读取包含尾随换行符的单行(如果存在于文件中),因此strtok()仅拆分单个项目。您应该尝试使用fread()读取整个文件,而不是fgets(),并且在strtok()循环之前null终止数组:

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

void le_arquivo(char *optarg) {
    FILE *respostas;                
    char *arq;                      
    char resp[SBUFF];              
    char *tokens[SBUFF];           
    int n, c;

    respostas = fopen(optarg, "r");
    if (respostas == NULL) {
        printf("Erro ao abrir o arquivo\n");
        return;
    }
    n = fread(resp, 1, sizeof(resp) - 1, respostas);
    resp[n] = '\0';
    arq = strtok(resp, "\n");
    for (c = 0; arq != NULL; c++) {
        tokens[c] = arq;
        arq = strtok(NULL, "\n");
    }
    fclose(respostas);
    organiza_dados(tokens, c);
}

#1


3  

strtok does not read from the file, it splits the character array. By definition fgets() reads a single line including the trailing newline if present in the file, so strtok() only splits a single item. Instead of fgets(), you should try and read the whole file with fread(), and null terminate the array before the strtok() loop:

strtok不读取文件,它会分割字符数组。根据定义,fgets()读取包含尾随换行符的单行(如果存在于文件中),因此strtok()仅拆分单个项目。您应该尝试使用fread()读取整个文件,而不是fgets(),并且在strtok()循环之前null终止数组:

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

void le_arquivo(char *optarg) {
    FILE *respostas;                
    char *arq;                      
    char resp[SBUFF];              
    char *tokens[SBUFF];           
    int n, c;

    respostas = fopen(optarg, "r");
    if (respostas == NULL) {
        printf("Erro ao abrir o arquivo\n");
        return;
    }
    n = fread(resp, 1, sizeof(resp) - 1, respostas);
    resp[n] = '\0';
    arq = strtok(resp, "\n");
    for (c = 0; arq != NULL; c++) {
        tokens[c] = arq;
        arq = strtok(NULL, "\n");
    }
    fclose(respostas);
    organiza_dados(tokens, c);
}