2

Good day to the community. The code I am trying to write must should read integers from a file while skipping lines that start with #. My problem is that no numbers are read and instead the it returns 0. The file looks like this:

#hello
#myname
#is
#file
122 4838
112   393949
1239 233
29393 44949
3 2
445 566

The output is:

0       0
Read 0 numbers
0       0
Read 0 numbers
0       0
Read 0 numbers
0       0
Read 0 numbers 

The code is:

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

typedef struct {
    int start;
    int end;
} path;
int main()
{
    int test; 
    path* array=malloc(sizeof(path));
    if(array==NULL) {
        printf("Error allocating memory\n");
        abort();
    }


    FILE* fd=fopen("Test.txt","r");
    if(fd==NULL) {
        printf("Error opening file\n");
        abort();
    }
    char buff[200];
    int counter=0;

    char c;
    while(fgets(buff,200,fd)&&counter<6) {
        c=buff[0];
        if(c=="#") {
            continue;
        }
        test=sscanf(&buff,"%d%d",array[counter].start,array[counter].end);
        printf("%d\t%d\n",array[counter].start,array[counter].end);
        printf("Read %d numbers\n", test);
        counter++;
    }

    fclose(fd);
    free(array);
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Azzarian
  • 153
  • 2
  • 13
  • You should not ask a new question by editing an already existing one, even if it seems similar to you. Any change which makes an existing answer look wrong should be avoidded. I am not sure whether I read your comments correctly. It seems that the two of you were lucky. In this special case you can keep it as is, but for the future please understand that editing when an answer is present is considered "moving target questions" and not at all appreciated. Please offer to @AdrianMole to rollback your recent edits to the question. – Yunnosch Jan 10 '20 at 17:51

1 Answers1

2

The problem in your code is in your arguments to the sscanf function. This requires the addresses of all variables that are the 'targets' for corresponding format fields (but reading in char[] strings is different, as the array name will decay to a pointer when used as a function argument).

So, in your case, to read in the two integer structure members, you should use this:

test = sscanf(buff, "%d%d", &array[counter].start, &array[counter].end);

Note 1: Also, you don't need the & operator on the buff argument, as this will decay, as mentioned above!

Note 2: Because the . (structure-member access operator) has a higher precedence than the & (address-of operator), the expression &array[counter].start is the same as &(array[counter].start) - but you may prefer the latter, more explicit code, as this can make things clearer for others to read and understand.

Feel free to ask for further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • :-) From the timing I guess you were already in the process of editing when I commented. Good work. – Yunnosch Jan 10 '20 at 07:50
  • And that with a single typo. Impressive. – Yunnosch Jan 10 '20 at 07:54
  • Thank you very much for pointing out. I would choose this answer but I stumbled into another problem. I would make another question but the problem is a bit similar so I decided to edit this one. – Azzarian Jan 10 '20 at 12:52
  • @Azzarian It's a simple typo: in `if(c=="#")...` you are comparing the single character, `c` to a string! Use this instead, and everything should be tickety-boo: `if(c=='#')…` (single quotes, not double). (Sorry that I missed it when posting my answer!) – Adrian Mole Jan 10 '20 at 12:57
  • @Adrian Mole you are a genious, thank you very much for your help. – Azzarian Jan 10 '20 at 13:48
  • As you can see, I explained to asker that "moving target questions" should be avoided. Consider letting them know that you are Ok with the question as is. (Assuming I got the situation right.) I see you as entitled to request rollback. In fact I normally rollback question edits like this, but I leave it up to you. You have my support either way. – Yunnosch Jan 10 '20 at 17:54