【问题标题】:Having garbage when reading a file and putting into a multi dimension array读取文件并放入多维数组时出现垃圾
【发布时间】:2021-11-28 15:40:41
【问题描述】:

我是学习 c 的新手,我正在尝试读取文件并将信息放入多维数组 multiar。

并最终使用该多维数组来创建结构

有艺术家的头衔和姓名和年份。

我尝试读取以 "," 分隔的文件,并将每个字符串放入 MD 数组的不同行中。

但是当我最终打印出数组时,我遇到了奇怪的垃圾。

我该如何解决这个问题?

file.text 就像

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

代码是

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

但输出是

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���>2+ �1
-----
  
-----
R�+ ��0���h�
-----
�1

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

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

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

但我希望输出是这样的

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

【问题讨论】:

  • 您应该在打印后显示您想要打印的内容以及您计划对数据执行的操作,因为除了将每行的字符串放入 @987654325 的数组中之外,我不明白您为什么需要其他任何东西@.
  • @JeffHolt 非常感谢,我编辑了
  • 您确实在您的第一个for 循环中为每个multiar 添加0x00 字符串终止符字符。在循环后添加multiar[i][len] = 0;
  • 打印时只使用 one 循环:for (s = 0; s &lt; i; s++) printf("-----\n%s\n", multiar[s]);

标签: c file multidimensional-array


【解决方案1】:

您的代码有 2 个问题:

  1. 您不是NUL 终止保存到multiar 的字符串。
  2. 您在输出时任意循环multiar 中的字符,而不是循环保存在那里的有效数据。
#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");
  }
}

请注意,这里没有检查 multiar 的越界访问,这是您应该实施的。

Demonstration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多