C printf-isn';我打印的号码不对
我把它作为我的结构C printf-isn';我打印的号码不对,c,printf,C,Printf,我把它作为我的结构 struct theFile{ int count; FILE *fPointer; int *fileItems[]; }myFile; 这是我在fileItems中保存文件的方法。它正在正确地保存数字。例如fileItems[0]=5,fileItems[1]=45,fileItems[2]=35 void saveFile(){ myFile.fPointer = fopen("m
struct theFile{
int count;
FILE *fPointer;
int *fileItems[];
}myFile;
这是我在fileItems中保存文件的方法。它正在正确地保存数字。例如fileItems[0]=5
,fileItems[1]=45
,fileItems[2]=35
void saveFile(){
myFile.fPointer = fopen("mileage.txt", "r");
int i = 0;
while (!feof(myFile.fPointer)){
myFile.fileItems[i] = (int*)malloc(sizeof(int));
fscanf(myFile.fPointer, " %d,", myFile.fileItems[i]);
i++;
}
myFile.count = i;
}
但是,当我用这种方法打印文件内容时,它会正确地打印第一个数字,但随后它会将其余数字打印为大数字。有人能告诉我为什么不能打印正确的数组内容吗
void viewFile(){
for(int i = 0; i < myFile.count; i++){
printf("%d, ", myFile.fileItems[i]);
}
}
void viewFile(){
对于(int i=0;i
另外请注意,它是用c编写的。在结构中进行声明
struct theFile{
int count;
FILE *fPointer;
int *fileItems[MAX];// MAX=10;
}myFile;
空数组下标无法知道数组如何指向。在结构中进行声明
struct theFile{
int count;
FILE *fPointer;
int *fileItems[MAX];// MAX=10;
}myFile;
空数组下标无法知道数组如何指向。在结构中进行声明
struct theFile{
int count;
FILE *fPointer;
int *fileItems[MAX];// MAX=10;
}myFile;
空数组下标无法知道数组如何指向。在结构中进行声明
struct theFile{
int count;
FILE *fPointer;
int *fileItems[MAX];// MAX=10;
}myFile;
空数组下标无法知道数组如何指向。
int*fileItems[]
等于int**fileItems代码>
很可能您需要的是整数数组,而不是指向整数的指针数组
将结构声明更改为int*fileItems代码>,并在循环前分配一次列表:
myFile.fileItems = malloc(sizeof(int) * initialNumberOfElements);
稍后,如果initialNumberOfElements
太小,则在realloc
中有更多空间:
myFile.fileItems = realloc(myFile.fileItems, sizeof(int) * biggerElementCount);
然后,fscanf
的参数必须是&myFile.fileItems[i]
不要忘记为分配功能失败的情况添加错误处理代码。您使用的任何文件函数也是如此:所有I/O都可能失败。int*fileItems[]
等于int**fileItems代码>
很可能您需要的是整数数组,而不是指向整数的指针数组
将结构声明更改为int*fileItems代码>,并在循环前分配一次列表:
myFile.fileItems = malloc(sizeof(int) * initialNumberOfElements);
稍后,如果initialNumberOfElements
太小,则在realloc
中有更多空间:
myFile.fileItems = realloc(myFile.fileItems, sizeof(int) * biggerElementCount);
然后,fscanf
的参数必须是&myFile.fileItems[i]
不要忘记为分配功能失败的情况添加错误处理代码。您使用的任何文件函数也是如此:所有I/O都可能失败。int*fileItems[]
等于int**fileItems代码>
很可能您需要的是整数数组,而不是指向整数的指针数组
将结构声明更改为int*fileItems代码>,并在循环前分配一次列表:
myFile.fileItems = malloc(sizeof(int) * initialNumberOfElements);
稍后,如果initialNumberOfElements
太小,则在realloc
中有更多空间:
myFile.fileItems = realloc(myFile.fileItems, sizeof(int) * biggerElementCount);
然后,fscanf
的参数必须是&myFile.fileItems[i]
不要忘记为分配功能失败的情况添加错误处理代码。您使用的任何文件函数也是如此:所有I/O都可能失败。int*fileItems[]
等于int**fileItems代码>
很可能您需要的是整数数组,而不是指向整数的指针数组
将结构声明更改为int*fileItems代码>,并在循环前分配一次列表:
myFile.fileItems = malloc(sizeof(int) * initialNumberOfElements);
稍后,如果initialNumberOfElements
太小,则在realloc
中有更多空间:
myFile.fileItems = realloc(myFile.fileItems, sizeof(int) * biggerElementCount);
然后,fscanf
的参数必须是&myFile.fileItems[i]
不要忘记为分配功能失败的情况添加错误处理代码。您使用的任何文件函数也是如此:所有I/O都可能失败。fscanf
请求一个指针作为参数,但它通常是现有的int
的地址,而不是“真实的”int*
。你可能想写:
struct theFile{
int count;
FILE *fPointer;
int fileItems[N]; // You need to put a value as N, like 10, or else the array will be of size 0
}myFile;
然后
这样,您就不需要malloc
和free
。在那之后,剩下的就好了
编辑:如果您不知道事先会有多少个int
,那么用户694733的答案更好。fscanf
请求一个指针作为参数,但它通常是现有int
的地址,而不是“真实的”int*
。你可能想写:
struct theFile{
int count;
FILE *fPointer;
int fileItems[N]; // You need to put a value as N, like 10, or else the array will be of size 0
}myFile;
然后
这样,您就不需要malloc
和free
。在那之后,剩下的就好了
编辑:如果您不知道事先会有多少个int
,那么用户694733的答案更好。fscanf
请求一个指针作为参数,但它通常是现有int
的地址,而不是“真实的”int*
。你可能想写:
struct theFile{
int count;
FILE *fPointer;
int fileItems[N]; // You need to put a value as N, like 10, or else the array will be of size 0
}myFile;
然后
这样,您就不需要malloc
和free
。在那之后,剩下的就好了
编辑:如果您不知道事先会有多少个int
,那么用户694733的答案更好。fscanf
请求一个指针作为参数,但它通常是现有int
的地址,而不是“真实的”int*
。你可能想写:
struct theFile{
int count;
FILE *fPointer;
int fileItems[N]; // You need to put a value as N, like 10, or else the array will be of size 0
}myFile;
然后
这样,您就不需要malloc
和free
。在那之后,剩下的就好了
编辑:如果您不知道事先会有多少个int
,那么user694733的答案更好。fileItems
是一个指针数组,因此您必须取消对它的引用两次。猜测一下:&(myFile.fileItems[i])
。但是看到您使用的fileItems
,最好将fileItems
声明为整数数组:int*fileItems
。然后相应地更改您的malloc
和fscanf
。您有没有理由检查fopen()
或malloc()
是否成功?顺便说一句,请输入malloc()
int*fileItems[]的返回值代码>是完全错误的。你来这里的目的是什么?一个潜在的无限数组?不会发生的while(!feof
也有错误,但请先理顺数组。Evert-我没有检查,因为我正在处理它。我