【问题标题】:can't I make strcpy after I have done atoi the token? [closed]完成 atoi 令牌后,我不能制作 strcpy 吗? [关闭]
【发布时间】:2021-03-30 14:02:09
【问题描述】:

您好,我在读取文件时遇到问题,该文件的组织方式如下:

id, name, nEst, lat, longi, adress

我在文件中有这个。

total number: 1
1, aaaaa, 0, 31.00, 21.72, nnn cccc

它正确地将行存储在缓冲区中,正确分离了 id,但是当它到达 strcpy (name, token); 时它会给出错误,在 strcpy 之前我对令牌进行了测试 printf,我注意到他保存了信息'santos' 正确,所以我不明白他为什么要窃听strcpy,之前完成atoi 将是一个问题,现在我必须做atoi 的相反操作吗?谢谢

void readMainFile(EMPRESA *emp){
    FILE * fp;
    fp= fopen("..\\data\\edificio.txt","r");
    int nEd = 0;
    int id = 0;
    int nEst = 0;
    float lat = 0.0f, longi = 0.0f;
    char name[50];
    char adrees[100];
    char buffer[200];

    if(fp!= NULL){
        //to know how many times I will read
        fscanf(fp,"%*s %*s %d\n",&nEd);
        for (int i = 0; i < nEd; i++) {
        while(fgets(buffer, 200, fp)){
            printf("->%s",buffer);
            char *token = strtok(buffer, ",");
            id = atoi(token);
            token = strtok(NULL, ",");
            strcpy(name, token);
            token = strtok(NULL, ",");
            nEst = atoi(token);
            token = strtok(NULL, ",");
            lat = atof(token);
            token = strtok(NULL, ",");
            longi = atof(token);
            token = strtok(NULL, ",");
            strcpy(adress, token);
            printf("ID: %d name: %s nEst: %d Lat: %.2f Longi: %.2f adress: %s\n",id,name,nEst,lat,longi,adress);
            insertInfo(emp,id,name,nEst,lat,longi,adress);
        }
    }
    fclose(fp);
    printf("Sucess!\n");
    }else{
        printf("ERROR!!\n");
  }
}

【问题讨论】:

  • 显示的代码应该可以工作(好吧,只要您的输入行格式完美,但我们现在不用担心)。请向我们展示一个完整的程序,我们可以自己编译和运行它并观察您遇到的相同问题。 (更多解释请参见stackoverflow.com/help/mcve。)
  • 了解您遇到的错误的完整且未经编辑的文本也非常有帮助。
  • 但是你还没有解释问题是什么。 “它给出错误”根本不够精确。什么错误?你如何测试它?
  • 谢谢,但是功能不够用。我对函数进行了尽可能少的更改,以将其变成一个可以编译和运行的完整程序——将void readMainFile(EMPRESA *emp)更改为int main(void),注释掉对insertInfo的调用,添加#includes——并且在您向我们展示的测试输入上运行它,它可以工作。它打印-&gt;1, santos, 0, 31.00, 21.72, rua brasil|ID: 1 Nome: santos nEst: 0 Lat: 31.00 Longi: 21.72 Morada: rua brasil||Sucess!(竖线显示换行符)。所以问题出在你还没有向我们展示的代码中。
  • 请提供minimal reproducible example。 (em português)。在 cmets 中给出有关问题的小提示无助于解决问题。

标签: c string file


【解决方案1】:

旁白:我赞同@zwol 的评论,确认所编写的代码在功能上能够读取您提供的输入并正确解析它。

也就是说,为了帮助调试工作,最好对strtok() 序列进行编码,以便在进行下一个调用之前测试对strtok() 的每次调用的返回。在调试时使用断点将有助于立即查看问题发生的位置。以下是您的代码适用于执行此操作(以及一些其他增强功能和样式建议):

 void readMainFile(EMPRESA *emp){
    int nEd = 0;
    int id = 0;
    int nEst = 0;
    float lat = 0.0f, longi = 0.0f;
    char nome[50] = {0};//initialize arrays
    char morada[100] = {0};
    char buffer[200] = {0};
    char *token = NULL;
    
    FILE * ficheiro_edificio;
    ficheiro_edificio = fopen("..\\data\\edificio.txt","r");

    if(ficheiro_edificio != NULL){
        //to know how many times I will read
        fscanf(ficheiro_edificio,"%*s %*s %*s %d\n",&nEd);
        for (int i = 0; i < nEd; i++) {
        while(fgets(buffer, 200, ficheiro_edificio)){
            printf("->%s",buffer);
             token = strtok(buffer, ",");
             if(token)
             {
                id = atoi(token);
                token = strtok(NULL, ",");
                if(token)
                {
                    strcpy(nome, token);
                    token = strtok(NULL, ",");
                    if(token)
                    {
                        nEst = atoi(token);
                        token = strtok(NULL, ",");
                        if(token)
                        {
                            lat = atof(token);
                            token = strtok(NULL, ",");
                            if(token)
                            {
                                longi = atof(token);
                                token = strtok(NULL, ",");
                                if(token)
                                {
                                    strcpy(morada, token);
                                }else {/*handle error*/}
                            }else {/*handle error*/}
                        }else {/*handle error*/}
                    else {/*handle error*/}}
                }else {/*handle error*/}
                printf("ID: %d Nome: %s nEst: %d Lat: %.2f Longi: %.2f Morada: %s\n",id,nome,nEst,lat,longi,morada);
                insertInfo(emp,id,nome,nEst,lat,longi,morada);
             }
    }
    fclose(ficheiro_edificio);
    printf("Sucess!\n");
    }else{
        printf("ERROR!!\n");
  }
}

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2021-09-16
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2018-10-18
    • 2013-06-13
    • 1970-01-01
    相关资源
    最近更新 更多