用C语言将数字从文件保存到数组
在我的代码中,我打开了文件(成功),并试图将数字输入到数组中,但它不起作用(控制输出不好)。编译器没有向我显示任何错误 txt文件上的链接:用C语言将数字从文件保存到数组,c,arrays,string,file,fopen,C,Arrays,String,File,Fopen,在我的代码中,我打开了文件(成功),并试图将数字输入到数组中,但它不起作用(控制输出不好)。编译器没有向我显示任何错误 txt文件上的链接: #包括 #包括 #包括 内部主(空){ 文件*fr_koty; int**array=NULL; int x=1;/*Pocet radku*/ int y=1;/*Pocet sloupcu*/ 字符行[1024]; 字符*辅助线; 整数;/*Promena pro cislo*/ 字符*选项卡; if((fr_koty=fopen(“koty.txt”
#包括
#包括
#包括
内部主(空){
文件*fr_koty;
int**array=NULL;
int x=1;/*Pocet radku*/
int y=1;/*Pocet sloupcu*/
字符行[1024];
字符*辅助线;
整数;/*Promena pro cislo*/
字符*选项卡;
if((fr_koty=fopen(“koty.txt”,“r”))==NULL){
printf(“Soubor se nepodario otevrit!”);
返回0;
}
while(fgets(第1023行,fr_koty)!=NULL){
数组=(int**)realloc(数组,x*sizeof(int*);
数组[x]=NULL;
辅助线=线;
而(sscanf(辅助线、%d、&number)==1){
数组[x]=(int*)realloc(数组[x],y*sizeof(int));
数组[x][y]=个数;
printf(“%d”,数组[x][y]);
if((tab=strhr(辅助线,'\t'))!=NULL){
辅助线=制表符+1;
y++;
}
否则{
打破
}
}
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++;
}
}
#包括
#包括
#包括
内部主(空){
文件*fr_koty;
int**array=NULL;
int x=1;/*Pocet radku*/
int y;/*Pocet sloupcu*/
字符行[1024];
字符*辅助线;
整数;/*Promena pro cislo*/
字符*选项卡;
if((fr_koty=fopen(“koty.txt”,“r”))==NULL){
printf(“Soubor se nepodario otevrit!”);
返回0;
}
while(fgets(第1023行,fr_koty)!=NULL){
y=1;
数组=(int**)realloc(数组,x*sizeof(int*);
数组[x-1]=NULL;
辅助线=线;
而(sscanf(辅助线、%d、&number)==1){
数组[x-1]=(int*)realloc(数组[x-1],y*sizeof(int));
数组[x-1][y-1]=个数;
printf(“%d”,数组[x-1][y-1]);
if((tab=strhr(辅助线,'\t'))!=NULL){
辅助线=制表符+1;
y++;
}
否则{
打破
}
}
putchar('\n');
x++;
}
}
您是否尝试过使用调试器?我看到的第一个大问题是,在完成内部时,您没有增加x
,并且在完成后,您从未将y
重置为0
。
#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++;
}
}