fopen没有这样的文件或目录

问题描述 投票:0回答:1

我的程序在加载文件并返回时始终失败:没有这样的文件或目录没有其他问题有帮助,因为其他人有不同的问题

incident *fileIn(incident* head){
    incident *new;  
    new = (incident *) malloc(sizeof(incident));
    if (new == NULL) {
        printf("No memory has been allocated the program will exit\n");
        exit(1);
    }
    FILE *fPointer;
    fPointer = fopen("input.txt","r");

    if (fPointer == NULL)
    {
        printf("Could not open file\n");
        perror("Err");
        exit(1);
    }

    new->next = new;
    char curr_line[300];

    while(fgets(curr_line, 10000, fPointer) != NULL){
new = (incident *) malloc(sizeof(incident));
        char *token=strtok(curr_line,";/");     /*auth xorizei thn eisodo kathe fora pou petixenei ; h / (gia tis imerominies)*/
        strcpy(new->coordinates.area,token);

        token=strtok(NULL, ";/");   
        new->reported.day=atoi(token);      /*h atoi metatrepei to string se int*/


        token=strtok(NULL, ";/");
        new->reported.month=atoi(token);

        token=strtok(NULL, ";/");
        new->reported.year=atoi(token);
token=strtok(NULL, ";");
        strcpy(new->url,token);

        incident* tail = head;
        if (head->next == head){
            head->next = new;
            new->next = head;

        }



        tail = tail->next;


        tail->next = new;
        new->next = head;
    }
    fclose(fPointer);
}

该文件在那里,我也向其添加了整个路径,但无济于事。任何帮助将不胜感激。几小时后我该怎么办,我已经尝试了所有我能想到的东西

c file fopen
1个回答
0
投票

编辑后,仍然存在一些问题。

输入

   new->next = new;

您创建了一个只有一个单元格的圈子列表,您不希望这样做,因为这样做之后:

while(fgets(curr_line, 10000, fPointer) != NULL){
   new = (incident *) malloc(sizeof(incident));

您有内存泄漏。您需要在需要的时候分配,您做得太早了,这些行

incident *new;  
new = (incident *) malloc(sizeof(incident));
if (new == NULL) {
    printf("No memory has been allocated the program will exit\n");
    exit(1);
}

必须移入while,并且必须删除new->next = new;

正在做:

char curr_line[300];

while(fgets(curr_line, 10000, fPointer) != NULL){

您允许fgets在一个数组中最多写入10000个字符,只允许包含300个字符,行为是未定义的,大小必须相同,例如:

char curr_line[300];

while(fgets(curr_line, sizeof(curr_line), fPointer) != NULL){

如果输入文件的内容错误,您就不会检查strtok返回的值strtok将返回NULL,并且您将具有未定义的行为。

[执行时:

strcpy(new->coordinates.area,token);

假设area是一个数组(聊天是incident的定义?),并且如果token长于area,则您没有任何保护。如果您再次有未定义的行为

在]中相同>

strcpy(new->url,token);

您也使用2次atoi

,但是如果token不是有效数字atoi静默返回0,我建议您使用实例strtol来检测错误,或至少例如if (sscanf(token, "%d", &new->reported.month) != 1) ...error...

您所有的列表管理错误

   incident* tail = head;
   if (head->next == head){
       head->next = new;
       new->next = head;

   }

   tail = tail->next;

   tail->next = new;
   new->next = head;

因为head->next == head肯定总是为假(您没有提示列表,并且您不想通过tail = tail->next;等创建圈子列表)>

您只想在列表的末尾添加,很可能head

可以为NULL。

也请注意,您的函数必须返回incident *很有可能是新的开头,但您永远不会返回return。]

您想要这样的东西:

new->next = NULL;

if (head == NULL)
  head = new;
else {
   incident* tail = head;

   while (tail->next != NULL)
     tail = tail->next;
   tail->next = new;
}

但是每转一圈要搜索列表的末尾是没有代价的,最好在while之前搜索最后一个单元格(如果head

不为NULL),然后更新列表中的tail。循环。

也不要忘记在函数结尾处执行return head;

我无法提供完整的代码,因为您没有提供事件的定义,您也不会谈论输入文件的内容=>这就是为什么第一句话是关于Minimal, Complete, and Verifiable example的原因>

© www.soinside.com 2019 - 2024. All rights reserved.