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