Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文本文件中添加整数,但它会打印出其他内容_C_Linked List_File Processing - Fatal编程技术网

C 从文本文件中添加整数,但它会打印出其他内容

C 从文本文件中添加整数,但它会打印出其他内容,c,linked-list,file-processing,C,Linked List,File Processing,我正在尝试将文本文件中的数据插入到链接列表中,该数据包含 ID-name 12345678-abcdefgh 但我不知道为什么ID总是打印奇怪的数字,而且我似乎在代码中找不到任何错误。是否有人知道是什么使ID打印出随机数,例如: -1069759088 在调试器中单步执行。@PaulOgilvie哪里可以找到调试器?此外,您只分配一个反复使用的节点。那可以,;不要对。要找到调试器,请查看桌子左下抽屉。或者检查IDE的文档。在malloc之后,您不需要将node->next归零。这将导致第二次

我正在尝试将文本文件中的数据插入到链接列表中,该数据包含

ID-name
12345678-abcdefgh
但我不知道为什么ID总是打印奇怪的数字,而且我似乎在代码中找不到任何错误。是否有人知道是什么使ID打印出随机数,例如:

-1069759088

在调试器中单步执行。@PaulOgilvie哪里可以找到调试器?此外,您只分配一个反复使用的
节点。那可以,;不要对。要找到调试器,请查看桌子左下抽屉。或者检查IDE的文档。在
malloc
之后,您不需要将
node->next
归零。这将导致第二次插入时出现未定义的行为。请将其放入调试器。@PaulOgilvie哪里可以找到调试器?此外,您只分配一个反复使用的
节点。那可以,;不要对。要找到调试器,请查看桌子左下抽屉。或者检查IDE的文档。在
malloc
之后,您不需要将
node->next
归零。这将导致第二次插入时出现未定义的行为。
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>

struct mhs{

    int nim;
    char name[1000];

    struct mhs *next;

};

int main(){

    struct mhs *head, *node, *curr;
    struct mhs *hashT[1000];
    int nim;
    char name[100];
    int i, size = 0;
    int choice;
    head = curr = NULL;

    node = (struct mhs *)malloc(sizeof(struct mhs));

    FILE *fp;
    fp = fopen("datamhs.txt", "r");
    if(fp != NULL){
        while(fscanf(fp, "%d-%[^\n]", &nim, name) != EOF){
            fflush(stdin);
            strcpy(node->name, name);
            node -> nim = nim;
            printf("%d\n", node->nim);
            printf("%s\n", node->name);

            system("pause");
            node -> next = NULL;
            if(head == NULL){
                head = node;
            } else {
                curr = head;
                while(curr -> next != NULL){
                    curr = curr -> next;
                }
                curr -> next = node;
            }
            size++;
        }
    } else {
        printf("Error, no file to read\n");
    }
-1069759088
abcdefgh