C 程序不';我跑不了。表示我正在尝试为指针赋值
我试图将文件读入数组,但代码没有运行。表示我正在尝试为指针赋值C 程序不';我跑不了。表示我正在尝试为指针赋值,c,C,我试图将文件读入数组,但代码没有运行。表示我正在尝试为指针赋值 #include <stdio.h> int main(void) { FILE *ifile; float num; float*numbers[8001]; float pointer = numbers; int i = 0; ifile = fopen("lotsOfNumbers.txt", "r"); if (ifile == NULL) {
#include <stdio.h>
int main(void)
{
FILE *ifile;
float num;
float*numbers[8001];
float pointer = numbers;
int i = 0;
ifile = fopen("lotsOfNumbers.txt", "r");
if (ifile == NULL) {
printf("File not found!! Exiting.\n");
}
else {
while (fscanf(ifile, "%f ", &num) != EOF) {
//printf("I read %d from the file. \n", num);
numbers[i] = #
if (i < 8801) {
i++;
}
else return 0;
}
}
fclose(ifile);
}
#包括
内部主(空)
{
文件*ifile;
浮点数;
浮点数*数字[8001];
浮点指针=数字;
int i=0;
ifile=fopen(“lotsOfNumbers.txt”、“r”);
如果(ifile==NULL){
printf(“找不到文件!!正在退出。\n”);
}
否则{
while(fscanf(ifile、%f、&num)!=EOF){
//printf(“我从文件中读取了%d。\n”,num);
数字[i]=&num;
如果(i<8801){
i++;
}
否则返回0;
}
}
fclose(ifile);
}
您的代码中有许多错误。你可能想要这个:
#include <stdio.h>
int main(void)
{
FILE *ifile;
float num;
float numbers[8001]; // you just need an array of float, no pointers needed
int i = 0;
ifile = fopen("lotsOfNumbers.txt", "r");
if (ifile == NULL) {
printf("File not found!! Exiting.\n");
}
else {
while (fscanf(ifile, "%f ", &num) != EOF) { // &num instead of num
printf("I read %f from the file.\n", num); // use %f for printing a float, not %d
numbers[i] = num;
if (i < 8001) { // 8001 instead of 8801
i++; // using a constant or a #define avoids this kind of errors
}
else
return 0;
}
fclose(ifile); // close file only if it has been opened sucessfully
}
// fclose(ifile); // that was the wrong place for fclose
}
#包括
内部主(空)
{
文件*ifile;
浮点数;
浮点数[8001];//您只需要一个浮点数数组,不需要指针
int i=0;
ifile=fopen(“lotsOfNumbers.txt”、“r”);
如果(ifile==NULL){
printf(“找不到文件!!正在退出。\n”);
}
否则{
而(fscanf(ifile,“%f”,&num)!=EOF){/&num而不是num
printf(“我从文件中读取了%f。\n”,num);//使用%f打印浮点,而不是%d
数字[i]=num;
如果(i<8001){//8001而不是8801
i++//使用常量或#define可避免此类错误
}
其他的
返回0;
}
fclose(ifile);//仅当文件已成功打开时才关闭文件
}
//fclose(ifile);//那地方放错了fclose
}
注释中有解释。什么是“代码未运行”?错误或输出错误?错误信息是什么?
浮点指针=数字代码>–你认为你在这里做什么?numbers[i]=&num代码>–这里呢?提示:如果你甚至不能编译你的程序,说它不运行是毫无意义的。