I have a homework and i can't really find where the problem with the code is. The main problem is to read 3 lines from a text file and use them to build a binary tree. The text file has these lines:
7
2 4 0 0 7 0 0
3 5 6 0 0 0 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
const char* p;
const char* v1;
const char* v2;
char buf[100];
FILE *fptr = fopen("sd.in", "r");
if (fptr == NULL) {
printf("Failed to open file\n");
return -1;
}
if(fgets(buf,100,fptr)!=NULL)
p=strtok(buf,"\n");
printf("%s\n", p);
while((p = strtok(NULL,"\n"))!=NULL)
{
printf("%s\n", p);
}
fclose(fptr);
return 0;
}
This is my code so far. When i compile it, it only shows the first line with number 7. How could i display all the lines? Thank you very much!
UPDATE of the code. Right now i can display the first and the second line but without number 2. I want to store the second line in v1 and the third line in v2.
if(fgets(buf,100,fptr)!=NULL)
p=strtok(buf,"\n");
printf("%s\n", p);
if((p = strtok(buf,"\n"))!=NULL && fgets(buf,100,fptr)!=NULL)
v1 = strtok(NULL,"\n");
printf("%s\n ",v1);