【问题标题】:fopen No such file or directoryfopen 没有这样的文件或目录
【发布时间】:2020-06-14 16:08:42
【问题描述】:

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

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);
}

文件在那里,我还添加了整个路径,但无济于事。任何帮助将不胜感激。几个小时后我能做什么,我已经尝试了所有我能想到的方法

【问题讨论】:

  • 没有足够的信息来回答。请构造一个minimal reproducible example,在其中显示您如何调用该程序。
  • 您必须小心大小写...确保在任何地方都使用小写字母。相对路径没问题...只要确保您的文件与 C 文件位于同一目录中...然后它应该可以工作...
  • 小花絮:在 C 中使用new 作为变量名是合法的,但是在转换为 C++ 时会成为一个绊脚石,因此将其视为保留字也不错。
  • 肯定fgets(curr_line, sizeof("input.txt"), fPointer) 是错误的;应该是sizeof curr_line
  • incident *new; if (new == NULL) { 具有未定义的行为,您期望这样做是什么?可能缺少malloc ...

标签: c file fopen


【解决方案1】:

在您的编辑之后仍然存在一些问题。

   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 在一个只允许包含 300 个字符的数组中写入最多 10000 个字符,行为未定义,大小必须相同,例如:

char curr_line[300];

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

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

做的时候:

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

假设 area 是一个数组(聊天是 incident 的定义?),如果 token 更长,您没有保护比区域,如果是这种情况,你又会有一个未定义的行为

中也是一样的
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 肯定总是假的(你没有cicle 列表),并且你不想创建一个圈子列表做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)然后更新在循环中。

另外别忘了在函数的最后加上return head;

我不能给出完整的代码,因为你没有给出事件的定义,你也没有谈论输入文件的内容 => 这就是为什么第一句话是关于Minimal, Complete, and Verifiable example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多