0

I am using some code I got from github and I was struggling to read a text file. The code is meant to read in some dimensions from a list of boxes. Here's a snippet. Please let me know if I need to add more.

void inputboxlist(void)
{
  short int n;
  int errnum;
  char lbl[5], dim1[5], dim2[5], dim3[5], boxn[5], strxx[5], stryy[5], strzz[5];

  strcpy(strtemp, filename);
  strcat(strtemp, ".txt");

  if ( (ifp=fopen(strtemp,"r")) == NULL )
  {
    errnum = errno;
    fprintf(stderr, "Value of errno: %d\n", errno);
    printf("Cannot open file %s", strtemp);
    fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
    exit(1);
  }
  tbn = 1;

  if ( fscanf(ifp,"%s %s %s",strxx, stryy, strzz) == EOF )
  {
    exit(1);
  }

  xx = atoi(strxx);
  yy = atoi(stryy);
  zz = atoi(strzz);

  while ( fscanf(ifp,"%s %s %s %s %s",lbl,dim1,dim2,dim3,boxn) != EOF )
  {
    boxlist[tbn].dim1 = atoi(dim1);
    boxlist[tbn].dim2 = atoi(dim2);
    boxlist[tbn].dim3 = atoi(dim3);

    boxlist[tbn].vol = boxlist[tbn].dim1 * boxlist[tbn].dim2 * boxlist[tbn].dim3;
    n = atoi(boxn);
    boxlist[tbn].n = n;

    while (--n)
    {
      boxlist[tbn+n] = boxlist[tbn];
    }
    tbn = tbn+atoi(boxn);
  }
  --tbn;
  fclose(ifp);
  return;
}

I then did a debug and as soon as I ran the char line(5th) my variables list was filled with strange symbols as seen below. Anyone know why it's doing this? excuse the picture it cant be copied (;

5
  • 5
    They just contain undetermined values, because you didn't initialize them (yet). Commented Jun 21, 2023 at 15:06
  • 2
    Uninitialized variables can contain anything. Why do you think this is strange?
    – Barmar
    Commented Jun 21, 2023 at 15:10
  • 1
    you can do = {0} to each one of those char arrays to initialize them all to 0 if you'd like.
    – yano
    Commented Jun 21, 2023 at 15:34
  • Local variables without explicit initialisation are not default initialised in C. Commented Jun 21, 2023 at 16:09
  • Okay thanks that makes sense. Its my first time using C so I wasn't aware that it wasn't default.
    – shongololo
    Commented Jun 22, 2023 at 7:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.