0

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);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 2
    You you want something to be done more than once, you need a loop. Your program, as written has a critical error: if `fgets` returns unsuccessfully, the variable `p` is not initialized, but the program continues as if nothing happened. – DYZ May 06 '17 at 00:21
  • 2
    You are only calling `fgets` once. `fgets` reads one line at a time. So not surprising that only the first line is printed. – kaylum May 06 '17 at 00:23
  • I have modifeid it now a little bit and now it prints a part of the second line,but without the first number – Răzvan Alexandru May 06 '17 at 00:25
  • 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); – Răzvan Alexandru May 06 '17 at 00:25
  • 1
    Please don't put code into the comments - it is unreadable. Edit your question and add the updated code. Probably best to leave the current code as well so that the question history is preserved. – kaylum May 06 '17 at 00:26
  • I want to store the second line in the char string v1, and the second one in char string v2 – Răzvan Alexandru May 06 '17 at 00:26
  • Or, make an array of strings and load each one in a for loop. – Michael Dorgan May 06 '17 at 00:27
  • The way it is written, you read `7` and then attempt to *tokenize* a line containing a single ASCII character `7` -- that's not going to work. You only need tokenize a *line* that contains *multiple-tokens* based on your *delimiter* string. (see the loop comments above) – David C. Rankin May 06 '17 at 00:33
  • Why use pointer to character for v1 and v2? Don't you need to store these values as a string? You need character array for v1 and v2 and to strcpy(). – Nguai al May 06 '17 at 01:33
  • like [this](http://ideone.com/hbQWCh) – BLUEPIXY May 06 '17 at 05:43

1 Answers1

-1

Here is the working code. Cannot use char pointer for v1 and v2 since memory address of buffer is not constant. They need to be stored into arrays like you mentioned in the title but your description says the other thing. Need to skip lines with just newline characters.

#include <stdio.h>
#include <string.h>
int
main(void)
{
   FILE *fp;
   char caBuffer[50];
   char caV1[50], caV2[50], caCnt[2];
   const char *cp;
   const char *cpDelimeter = "\n";
   int iLoopCnt = 0;

   if( ( fp = fopen( "xyz.txt", "r" ) ) == NULL ){
      printf( "failed opening\n" );
      return 1;
   }

   while( fgets( caBuffer, sizeof(caBuffer), fp ) != NULL ){
      //skip the lines with newline char
      if( strlen(caBuffer) > 1 )
      {  
         cp = strtok(caBuffer, cpDelimeter);

         if( cp != NULL ){
            switch( iLoopCnt++ )
           {
              case 0:
                  strcpy(caCnt, cp );
                  break;
              case 1:
                  strcpy(caV1, cp );
                  break;
              case 2:
                  strcpy(caV2, cp );
                  break;
            }
        }
    }
}

printf("caCnt = %s\n", caCnt );
printf("caV1  = %s\n", caV1 );
printf("caV2  = %s\n", caV2 );

fclose(fp);

return 0;

}

Updates were made per suggestion below. Thanks.

Nguai al
  • 958
  • 5
  • 15