Home > database >  Having garbage when reading a file and putting into a multi dimension array
Having garbage when reading a file and putting into a multi dimension array

Time:10-09

I am new to learning c, and I am trying to read a file and put the infomation into an multidimensional array multiar.

And eventually use that multidimensional array to create struct that

has title and name of artist and year.

I tried to read the file separated by "," , and put each string into different rows of MD array.

But I am having weird garbage when I finally print out the array.

How do I solve this problem?

file.text is like

All Along the Watchtower,Bob Dylan,1968,Mercedes Benz,Janis Joplin,1971,Stairway to Heaven,Led Zeppelin,1971,

the code is

int main(){
char str[255];
  char* token;
  char multiar[50][50];
  FILE *fptr;
  fptr = fopen("file.text" ,"r");
  fgets(str, 100, fptr);
  token = strtok(str, ",");
  //so this token now has "All Along the Watchtower"
  int i = 0,j;
  while (token !=NULL){
    int len = strlen(token);
    for (j=0;j<len;j  ){
      multiar[i][j] = token[j];
      
      
    }
    token = strtok(NULL,",");
    i  ;
  }
  int s, t;
  for (s=0;s<30;s  ){
    for (t=0;t<30;t  ){
      printf("%c",multiar[s][t]);
    }
    printf("\n-----\n");
  }
}

but the output is

All Along the Watchtower
-----
Bob Dylan
-----
19680�s����s����s����
-----
Mercedes Benz��LP��s�
-----
Janis Joplin���@-���
-----
1971 �   �      x0���
-----
Stairway to Heaven �     �
-----
Led Zepp���-����Q
-----
��q���b x0���
-----
���(��  �@�
-----
P�      �v      �b
-----
�Zx0���`.����  
-----

-----

-----

-----

-----
�/�����  �
-----
(�i���/���  �
-----
 1���0���>21
-----
  
-----
R�  ��0���h�
-----
�1

-----
9�/ �@�
-----
1P@2

-----
$
-----
�����1
       
-----
�&�; �
-----
����
-----

-----
`�p5���x5���
-----

But I want the output to be like

All Along the Watchtower
-----
Bob Dylan
-----
1968
-----
Mercedes Benz
-----
Janis Joplin
-----
1971
-----
Stairway to Heaven
-----
Led Zepp
-----
1971
-----

CodePudding user response:

You have 2 problems with your code:

  1. You're not NUL terminating the strings saved to multiar.
  2. You're arbitrarily looping over characters in multiar on output rather than looping over the valid data saved there.
#include <stdio.h>
#include <string.h>

int main(){
  char str[255] = "All Along the Watchtower,Bob Dylan,1968,Mercedes Benz,Janis Joplin,1971,Stairway to Heaven,Led Zeppelin,1971,";
  char* token;
  char multiar[50][50];
  token = strtok(str, ",");
  //so this token now has "All Along the Watchtower"
  int i = 0,j;
  while (token !=NULL){
    int len = strlen(token);
    // could use strcpy, strncpy, memcpy, etc here instead of manually
    // copying each character.
    for (j=0;j<len;j  ){
      multiar[i][j] = token[j];
    }

    // NUL terminate the string here
    multiar[i][j] = '\0';
    token = strtok(NULL,",");
    i  ;
  }

  int s, t;
  // loop over i, that's how many words are in your array
  for (s=0;s<i;s  ){
    int len = strlen(multiar[s]);
    // loop over the string length for each string at multiar[i]
    // could forgo this loop entirely and just printf("%s", multiar[s]) instead
    for (t=0;t<len;t  ){
      printf("%c",multiar[s][t]);
    }
    printf("\n-----\n");
  }
}

Be warned, there's no checking here for out-of-bounds access on multiar, that's something you should implement.

Demonstration

  • Related