【问题标题】:Problems writing in array after malloc Cmalloc C之后在数组中写入的问题
【发布时间】:2015-08-31 14:34:20
【问题描述】:

在使用 malloc 函数分配内存后,我在尝试写入由我编写的结构的数组位置时遇到了一些问题。我必须从 file.txt 中读取一些数据(行格式:int string char。这些是一个人的 id,a非特定字符串和一个字符,它给出要读取和保存的字符串的“类型”),每行一行,并保存在结构数组中。不幸的是,通常我必须将数据添加到结构数组中的前一个索引,但似乎程序不允许我写入结构数组的前一个索引。我不知道为什么。 希望我足够清楚。感谢您的帮助!

以下是 file.txt 的示例:

    6         //number of lines
    3 tPar P
    2 tPar P
    3 tArr A
    1 tPar P
    1 tArr A
    2 tArr A

代码如下:

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

#define DBG printf("Ok linea %d\n", __LINE__);


typedef enum { 
    false, 
    true 
} bool;

struct file {
    char name[15];
    int n;
    struct record *rec;
};

struct record {
    int cod;
    char tPar[10];
    char tArr[10];
};


int read(struct file *f) {

    FILE *fd;
    char str[100];
    int pCod;
    char pTime[10];
    char pChar;
    bool found;
    int i = 0;
    int j;

    fd = fopen(f->name, "r");

    if(!fd) {
        printf("Errore di apertura file.\n");
        exit(1);
    }

    fscanf(fd, "%d\n", &f->n);

    f->rec = malloc((f->n)*sizeof(struct record));

    if(f->rec == NULL) {
        printf("Errore nell'allocazione della memoria.\n");
        exit(1);
    }

    while(fgets(str, 100, fd)) {

        sscanf(str, "%d %s %c", &pCod, pTime, &pChar);
        pChar = toupper(pChar);
        found = false;

        f->rec[i].cod = 0;                  // these 4 lines are needed just
        strcpy(f->rec[i].tPar, "xxx");      // to check if it writes in the
        strcpy(f->rec[i].tArr, "xxx");      // struct array

        for(j=0; j<i; j++) {
            if((pCod == f->rec[j].cod) && (pChar == 'P')) {
                strcpy(f->rec[j].tPar, pTime);  // doesn't copy
                found = true;                   
            }
            if((pCod == f->rec[j].cod) && (pChar == 'A')) {
                strcpy(f->rec[j].tArr, pTime);  // doesn't copy
                found = true;                   
            }
        }

        if(!found) {
            f->rec[i].cod = pCod;
            if(pChar == 'P')
                strcpy(f->rec[i].tPar, pTime);
            if(pChar == 'A')
                strcpy(f->rec[i].tArr, pTime);

        }

        if(pChar == 'P')
            printf("%d %s %c\n", f->rec[i].cod, f->rec[i].tPar, pChar);
        if(pChar == 'A')
            printf("%d %s %c\n", f->rec[i].cod, f->rec[i].tArr, pChar);

        i++;

    }

    fclose(fd);

    return 0;

}


int main(int argc, char *argv[]) {

    struct file f;

    strcpy(f.name, argv[1]);

    read(&f);

    return 0;
}

预期输出:

3 tPar P
2 tPar P
3 tArr A
1 tPar P
1 tArr A
2 tArr A

实际输出:

3 tPar P
2 tPar P
0 xxx A
1 tPar P
0 xxx A
0 xxx A

【问题讨论】:

  • 不要键入def bool。取而代之的是#include &lt;stdbool.h&gt;!
  • 你为什么要做i++?如果您只在 if (!found) 中这样做,您似乎可能会在 for(f=0 ... for 循环中遇到分段错误。看起来 i 用于跟踪记录数,而不是您已阅读的行数。
  • @chux,是的,现在我明白了你在说什么。我添加该 commet 只是为了让您清楚,它不存在于原始 .txt 中。
  • 仍在if(!found) { 之外使用i++;,还是您像@JustinDanielson 建议的那样把它放在那里?在更正此问题之前,代码将失败。
  • 大家好!有用!抱歉,@MattMcNabb,我真的认为 malloc 是问题所在。谢谢你们!感谢您的帮助!

标签: c arrays struct malloc


【解决方案1】:

我怀疑你的 bool 枚举值没有编译为 0 和 1,所以你的测试

if(!found) {

无法正常工作。 (不过,我现在无法真正测试我的理论)。

【讨论】:

  • 如果我检查过并且我的 bool 工作正常,则问题不在于那个特定的问题。
  • enums 被定义为第一个是0,后续的1大于前一个(除非指定了特定值)
  • @Matt,Doh!是的,你说得很对。当我不记得它是否从 1 点开始时,我去查了一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多