使用fgets()和strtok()在C中逐行读取文件? [英] Using fgets() and strtok() to read in a file line-by-line in C?

查看:67
本文介绍了使用fgets()和strtok()在C中逐行读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用fgets和strtok()逐行读取文件,并创建每个不同信息行的链接列表.

I'm trying to use fgets and strtok() to read in a file line by line, and create a linked list of each different line of information.

现在,我只是将信息放入一个数组中,只是为了试图弄清楚如何正确地读取信息,但是它不能正常工作.

Right now, I'm only just putting the information into an array, just to try to figure out how to read the information in correctly, but it's not working right.

在while(fgets)部分中,似乎可以将所有内容正确加载到数组中并打印出来.但是,在执行完该循环并尝试打印出整个数组之后,我得到的结果确实很奇怪...这仅是最后一行的一部分,而大部分都不是完整的单词或其他任何内容.

In the while(fgets) portion, it seems to load everything into the array properly, and prints it out. However, after that loop has executed and I try to print out the whole array, I get really weird results.. which is mostly portions of the last line ONLY, and not complete words or anything for the most part.

例如,如果我正在阅读:

For example, if I'm reading in:

Simpson, Homer, Male, 1976
Simpson, Marge, Female, 1978
Simpson, Bart, Male, 2002 
Simpson, Lisa, Female, 2004 
Simpson, Maggie, Female, 2011 

最后我得到的打印输出是这样的:

The printout I get at the end is something like:

le
Simpson
 Maggie


Simpson
 Maggie
e
ale
Simpson
 Maggie
e
e
Simpson
 Maggie
 Female
 2011

请让我知道我要去哪里了,谢谢!

Please let me know where I'm going wrong, thanks!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRINGSIZE 10
#define LINESIZE 128

struct person{
    char firstName[MAXSTRINGSIZE];
    char lastName[MAXSTRINGSIZE];
    char gender[MAXSTRINGSIZE];
    int birthYear;
    struct person *next;
} *first, *current;


int main (void){
    static const char filename[] = "Assignment1file.txt";
    FILE *myfile = fopen ( "Assignment1file.txt", "r" );

    int i=0;
    int j=0;
    int k=0;
    int l=0;
    char *result[10][4];
    char line[LINESIZE];
    char *value;

    for(i=0; i<9; i++){
        for(j=0;j<4;j++){
            result[i][j] = NULL;
        }
    }
    i=0;

    // loop through each entry in Assignment1file
    while(fgets(line, sizeof(line), myfile)){

        //load last name
        value = strtok(line, ",");
        result[i][0] = value;
        printf("%i 0 %s", i, value);


        //load first time
        value = strtok(NULL, ",");
        result[i][1] = value;
        printf("%i 1 %s", i, value);

        // load gender
        value = strtok(NULL, ",");
        result[i][2] = value;
        printf("%i 2 %s", i, value);

        // load birth year
        value = strtok(NULL, "\n");
        result[i][3] = value;
        printf("%i 3 %s", i, value);
        printf("\n");

        for(j=0;j<4;j++){
            printf("%s\n", result[i][j]);
        }


        //go to next line
        i++;
    }   

    // read out the array
    for(k=0; k<5; k++){
        for(j=0;j<4;j++){
            printf("%s\n", result[k][j]);
        }
    }

    fclose(myfile);
    return 0;
}

推荐答案

strtok()返回 line [] 内的指针,因此当您阅读下一行时,所有指针您保存的文件现在指向文件最后一行的存储位置.

strtok() returns pointers inside line[] so when you read the next line all the pointers you saved are now pointing to places where the last line of the file is stored.

您可以像这样为字符串的每一位分配内存:

You could allocate memory for each bit of string like so:

//load last name
value = strtok(line, ",");
result[i][0] = malloc(strlen(value) + 1);
strcpy(result[i][0], value);

顺便说一句,您不需要一开始就将所有内容都设置为 NULL 的循环,您可以执行以下操作:

As an aside, you don't need the loops at the start to set everything to NULL, you could do this instead:

char *result[10][4] = {0};

这篇关于使用fgets()和strtok()在C中逐行读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆