在C中将数字从文件保存到数组
Saving numbers from file to array in C
在我的代码中,我打开了文件(成功),我试图将数字放入数组,但它不起作用(控制输出错误)。编译器没有显示任何错误。
link 在 txt 文件上:https://textuploader.com/1amip
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fr_koty;
int **array = NULL;
int x = 1; /* Pocet radku */
int y = 1; /* Pocet sloupcu */
char line[1024];
char *assistant_line;
int number; /* Promena pro cislo*/
char *tab;
if((fr_koty = fopen("koty.txt", "r")) == NULL) {
printf("Soubor se nepodarilo otevrit!");
return 0;
}
while(fgets(line, 1023, fr_koty) != NULL) {
array = (int **) realloc(array, x * sizeof(int *));
array[x] = NULL;
assistant_line = line;
while(sscanf(assistant_line, "%d", &number) == 1) {
array[x] = (int *) realloc(array[x], y * sizeof(int));
array[x][y] = number;
printf("%d ", array[x][y]);
if((tab = strchr(assistant_line, '\t')) != NULL) {
assistant_line = tab + 1;
y++;
}
else {
break;
}
}
putchar('\n');
x++;
}
}
数字的输出是随机的。我认为原因是使用内存不好,但我看不出问题。
您正在将 x 和 y 初始化为 1,这对于 realloc 来说是可以的,但是由于 C 数组是基于 0 的,因此您需要使用 x-1 和 y-1 来访问数组元素。
或者您将它们初始化为 0 并在 realloc 调用中使用 (x+1) 和 (y+1)。我更喜欢这种方式。
现在我也看到了。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fr_koty;
int **array = NULL;
int x = 1; /* Pocet radku */
int y; /* Pocet sloupcu */
char line[1024];
char *assistant_line;
int number; /* Promena pro cislo*/
char *tab;
if((fr_koty = fopen("koty.txt", "r")) == NULL) {
printf("Soubor se nepodarilo otevrit!");
return 0;
}
while(fgets(line, 1023, fr_koty) != NULL) {
y = 1;
array = (int **) realloc(array, x * sizeof(int *));
array[x-1] = NULL;
assistant_line = line;
while(sscanf(assistant_line, "%d", &number) == 1) {
array[x-1] = (int *) realloc(array[x-1], y * sizeof(int));
array[x-1][y-1] = number;
printf("%d ", array[x-1][y-1]);
if((tab = strchr(assistant_line, '\t')) != NULL) {
assistant_line = tab + 1;
y++;
}
else {
break;
}
}
putchar('\n');
x++;
}
}
在我的代码中,我打开了文件(成功),我试图将数字放入数组,但它不起作用(控制输出错误)。编译器没有显示任何错误。
link 在 txt 文件上:https://textuploader.com/1amip
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fr_koty;
int **array = NULL;
int x = 1; /* Pocet radku */
int y = 1; /* Pocet sloupcu */
char line[1024];
char *assistant_line;
int number; /* Promena pro cislo*/
char *tab;
if((fr_koty = fopen("koty.txt", "r")) == NULL) {
printf("Soubor se nepodarilo otevrit!");
return 0;
}
while(fgets(line, 1023, fr_koty) != NULL) {
array = (int **) realloc(array, x * sizeof(int *));
array[x] = NULL;
assistant_line = line;
while(sscanf(assistant_line, "%d", &number) == 1) {
array[x] = (int *) realloc(array[x], y * sizeof(int));
array[x][y] = number;
printf("%d ", array[x][y]);
if((tab = strchr(assistant_line, '\t')) != NULL) {
assistant_line = tab + 1;
y++;
}
else {
break;
}
}
putchar('\n');
x++;
}
}
数字的输出是随机的。我认为原因是使用内存不好,但我看不出问题。
您正在将 x 和 y 初始化为 1,这对于 realloc 来说是可以的,但是由于 C 数组是基于 0 的,因此您需要使用 x-1 和 y-1 来访问数组元素。
或者您将它们初始化为 0 并在 realloc 调用中使用 (x+1) 和 (y+1)。我更喜欢这种方式。
现在我也看到了。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fr_koty;
int **array = NULL;
int x = 1; /* Pocet radku */
int y; /* Pocet sloupcu */
char line[1024];
char *assistant_line;
int number; /* Promena pro cislo*/
char *tab;
if((fr_koty = fopen("koty.txt", "r")) == NULL) {
printf("Soubor se nepodarilo otevrit!");
return 0;
}
while(fgets(line, 1023, fr_koty) != NULL) {
y = 1;
array = (int **) realloc(array, x * sizeof(int *));
array[x-1] = NULL;
assistant_line = line;
while(sscanf(assistant_line, "%d", &number) == 1) {
array[x-1] = (int *) realloc(array[x-1], y * sizeof(int));
array[x-1][y-1] = number;
printf("%d ", array[x-1][y-1]);
if((tab = strchr(assistant_line, '\t')) != NULL) {
assistant_line = tab + 1;
y++;
}
else {
break;
}
}
putchar('\n');
x++;
}
}