0

My program keeps failing while loading a file that is there and returns: No such file or directory No other questions have been any help because others had different issues

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

The file is there and I also added the whole path to it but to no avail. Any help would be greatly appreciated. What can I do it's due in a few hours and I've tried every thing I could think of

whatevahhh
  • 47
  • 6
  • 1
    There's not enough information to answer. Please construct a [mre] where you show how you call the program. – klutt Jun 14 '20 at 16:11
  • You must be careful about cases... Be sure that you use lower case everywhere. Relative paths are OK... Just be sure that you have file in the same directory as your C file...Then it should work... – XraySensei Jun 14 '20 at 16:12
  • 1
    Minor tidbit: using `new` as a variable name in C is legal, but it will a stumbling block when converting to C++, so considering it a reserved word is not a bad idea. – Steve Friedl Jun 14 '20 at 16:13
  • 1
    For sure `fgets(curr_line, sizeof("input.txt"), fPointer)` is wrong; should be `sizeof curr_line` instead. – Steve Friedl Jun 14 '20 at 16:14
  • `incident *new; if (new == NULL) {` has undefined behavior, what did you expect doing that ? Probably a `malloc` is missing ... – bruno Jun 14 '20 at 16:25
  • ```sizeof curr_line``` didn't do and I do have the file in the same directory – whatevahhh Jun 14 '20 at 16:26
  • @whatevahhh you did not understans the remark, `sizeof("input.txt")` values the size of a pointer (so 4 or 8 typically) , you wanted the size of the array so `sizeof(curr_line)` valuing 300 – bruno Jun 14 '20 at 16:27
  • *new gets defined later on and its being used for a linked list – whatevahhh Jun 14 '20 at 16:28
  • @whatevahhh you cannot test the value of a variable *before* to initialize it ! – bruno Jun 14 '20 at 16:29
  • @bruno That did the trick and the file is being read correctly thanks! However the tokens are not being saved to the list properly but thats something else – whatevahhh Jun 14 '20 at 16:33
  • @whatevahhh you can update your question editing it for more – bruno Jun 14 '20 at 16:35
  • @bruno I added more of my code to show what is being done with the list elements ```head``` and ```new``` – whatevahhh Jun 14 '20 at 16:38
  • @whatevahhh `if (head->next == head)` is very probably always false except if you have a circle list with only one cell. Then `incident* tail = head; ... while (tail->next != head){` the test in while has no chance to be true a day – bruno Jun 14 '20 at 16:40
  • @whatevahhh you also missed to correct `sizeof("input.txt")` – bruno Jun 14 '20 at 16:42
  • That if statement is there only for the one cell also i removed the while instance but no thigh has changed – whatevahhh Jun 14 '20 at 16:46
  • @whatevahhh I will put an answer, stop to edit your question ... – bruno Jun 14 '20 at 16:47
  • @whatevahhh I put my answer – bruno Jun 14 '20 at 17:14

1 Answers1

0

After your edit there are still several problems.

In

   new->next = new;

you create a circle list with only one cell, you do not want that, and because just after you do :

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

you have a memory leak. You need to allocate when needed, you did too earlier, the lines

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

must be moved into the while and the line new->next = new; must be removed

Doing :

char curr_line[300];

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

you allow fgets to write up to 10000 characters in an array allowing to contains only 300, the behavior is undefined, the size must be the same, for instance :

char curr_line[300];

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

You never check the value return by strtok if the input file has a wrong content strtok will return NULL and you will have an undefined behavior.

When doing :

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

that supposes area is an array (chat is the definition of incident ?) , and you have no protection in case token is longer than area, if it is the case you have again an undefined behavior

It is the same in

strcpy(new->url,token);

You also use 2 times atoi, but in case the token is not a valid number atoi silently returns 0, I recommend you to use for instance strtol to detect an error, or at least for instance if (sscanf(token, "%d", &new->reported.month) != 1) ...error...

All your list management is wrong in

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

   }

   tail = tail->next;

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

because head->next == head is certainly always false (you do not have a cicle list), and you do not want to create a circle list doing tail = tail->next; etc

You just want to add at the end of the list, and very probably head can be NULL.

Note also your function have to return an incident * being very probably the new head, but you never return.

You want something like that :

new->next = NULL;

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

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

but to search for the end of the list each turn is expensive for nothing, better to search for the last cell (if head not NULL) before the while then to update tail in the loop.

Also do not forget to do return head; at the end of the function.

I cannot give a full code because you do not give the definition of incident, you also do not speak about the content of the input file => this is why the very first remark is about the Minimal, Complete, and Verifiable example

bruno
  • 32,421
  • 7
  • 25
  • 37