以前存储的字符串被fgets覆盖 [英] Previously stored strings are overwritten by fgets

查看:257
本文介绍了以前存储的字符串被fgets覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fgets()从CSV文件读取记录,一次读取一行,然后使用strtok()解析每一行中的字段.我遇到了一个问题,其中fgets()覆盖了先前写入的字符串,而使用了新字符串.
这是我的意思的示例:

I'm reading records from a CSV file using fgets() to read the file one line at a time, and strtok() to parse the fields in each line. I'm encountering a problem where fgets() overwrites a string that was previously written, in favor of the new string.
Here's an example of what I mean by that:

record.csv(这是我正在读取的文件)

record.csv (This is the file I'm reading in)

John,18
Johann,29

main.c

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

typedef struct customer {
    char *name;
    int age;
} Customer;

int main(void) 
{
    FILE *csv_data;
    char line[100], *token;
    Customer newData[2];

    csv_data = fopen("record.csv", "r");
    // Index 0 for John's data, index 1 for Johann's data
    int i = 0;

    /* loops until end of file */
    while(fgets(line, 100, csv_data)) {

        /* name field */
        token = strtok(line, ",");
        if (token != NULL) {
            newData[i].name = token;        
        }

        /* age field */
        token = strtok(NULL, ",");
        if (token != NULL) {
            // atoi() converts ascii char to integer
            newData[i].age = atoi(token);
        }
        i++;
    }
    /* print John's records */
    printf("%s\n", newData[0].name);
    printf("%d\n", newData[0].age);

    /* print Johann's records */
    printf("%s\n", newData[1].name);
    printf("%d\n", newData[1].age);

    return 0;
}


当我们编译并执行此代码时,它会打印出来:


When we compile and execute this, it prints out:

Johann
18
Johann 
29

while循环的第二次迭代过程中,newData[0].name中的

"John"被"Johann"覆盖.但是请注意,只有字符串混合在一起,而不是整数.我怀疑这与fgets有关,因为当我修改上述源代码以使其仅运行fgets一次时,"John"的输出应为应有的状态.
也许我在滥用fgets(或者我的假设是错误的),但是有人可以给我一些关于为什么每次调用fgets都会覆盖字符串的提示吗?

"John" in newData[0].name gets overwritten with "Johann" during the second iteration of the while loop. Notice however that only the strings get mixed up, but not the integers. I suspect this has to do with fgets because when I modified the above source to only run fgets once, the output for "John" was as it should be.
Maybe I'm misusing fgets (or perhaps my assumption is wrong), but could someone give me some pointers on why the strings are being overwritten with each calls to fgets?

第二次更新:非常感谢所有评论者和回答者.很高兴知道那些我没有意识到的事情.源现在可以正常工作了.

Second Update: Thank-you very much again to all the commenters and the answerers. Good to know those things which I was not aware of. The source works perfect now.

推荐答案

您不是在复制字符串,而是指向字符串的指针.

You are not copy the string but the pointer to the string.

一种非常简单的复制字符串的方法,但是请注意,这会将字符串的大小限制为99个字符.

A very simple way to copy the string, but note that this limit the size of the string at 99 characters.

typedef struct customer {
    char name[100];
    int age;
} Customer;

strcpy(newData[i].name, token);   

这篇关于以前存储的字符串被fgets覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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