Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将文件中的整数指定给数组_C_Arrays_File Io_While Loop - Fatal编程技术网

C 将文件中的整数指定给数组

C 将文件中的整数指定给数组,c,arrays,file-io,while-loop,C,Arrays,File Io,While Loop,编辑:尝试了一些建议,并没有改变太多,但它给了我不正确的数据,但至少他们不是零。谢谢大家的帮助 我正在编写代码,用于从文本文件中读取成绩并将其存储在数组中。我的完整代码相当大,所以我只发布我有问题的部分;我的while循环不更改数组中的值。样本数据为659987768937-999,全部在单独的行中。我的输出告诉我,在函数应该更改其值之后,数组值仍然为零。其他while循环都是对同一任务的失败尝试 int readGrades(double grades[]){ FILE* gd;

编辑:尝试了一些建议,并没有改变太多,但它给了我不正确的数据,但至少他们不是零。谢谢大家的帮助

我正在编写代码,用于从文本文件中读取成绩并将其存储在数组中。我的完整代码相当大,所以我只发布我有问题的部分;我的while循环不更改数组中的值。样本数据为659987768937-999,全部在单独的行中。我的输出告诉我,在函数应该更改其值之后,数组值仍然为零。其他while循环都是对同一任务的失败尝试

int readGrades(double grades[]){
    FILE* gd;
    int count = 0;
    double ind;
    gd = fopen("sample.txt", "r");

    while(count < MAX){ //MAX is defined as 100/array is grades[MAX]
    fscanf(gd, "%lf", &ind); //Should scan through items in the file 
    if(ind < 0) //End of sample data is -999 this breaks out of the loop
        break;
    grades[count] = ind; //changes values in the array (it doesn't change them from 0)
    count++; } //increments the index of the array

   /*while(fscanf(gd, "%lf", &ind)>0){
    grades[count] = ind;
    count++; }*/

   /*do {
        fscanf(gd, "%lf", &grades[count]);
        printf("%.0lf", grades[count]);
        if(fscanf(gd, "%lf", &grades[count])== -999){
            break;
        }
        count++;
   }while(fscanf(gd, "%lf", &grades[count])> 0);*/
    fclose(gd);
    return count+1;
}
就在主楼里面。#定义最大值100和双坡[MAX]在干管外。我返回count+1,因为函数需要返回读取的数据项的数量

有人要求提供完整的程序:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int readGrades(double []);
void frequency(double [], int);
int maximum(double [], int);
int minimum(double [], int);
int deleteElement(double [], int, int);
double mean(double [], int);
double standardDeviation(double [], int);
#define MAX 100
double grades[MAX];
int loc;

int main(){
    int numGrades;
    printf("%lf", grades);
    numGrades = readGrades(grades);
    loc = minimum(grades, numGrades);
    numGrades = deleteElement(grades, numGrades, loc);
    printf("The data has been adjusted by removing the minimum %.2lf\n", grades[loc]);
    loc = maximum(grades, numGrades);
    numGrades = deleteElement(grades, numGrades, loc);
    printf("The data has been adjusted by removing the maximum %.2lf\n", grades[loc]);
    printf("The adjusted mean is %.2lf\n", mean(grades, numGrades));
    printf("The adjusted standard deviation is %.2lf\n",       standardDeviation(grades, numGrades));
    printf("Here is a histogram of the adjusted data:\n");
    frequency(grades, numGrades);
    return 0;
}
int readGrades(double grades[]){
    FILE* gd;
    int count = 0;
    double ind;
    gd = fopen("sample.txt", "r");
    while(count < MAX){
        fscanf(gd, "%lf", &ind);
    //double atod(ind);
        if(ind < 0)
           break;
    grades[count] = ind;
    count++;
}

/*while(fscanf(gd, "%lf", &ind)>0){
    grades[count] = ind;
    count++;
}*/
/*do {
        fscanf(gd, "%lf", &grades[count]);
        printf("%.0lf", grades[count]);
        if(fscanf(gd, "%lf", &grades[count])== -999){
            break;
        }
        count++;
}while(fscanf(gd, "%lf", &grades[count])> 0);*/
printf("%.0lf", grades[1]);
fclose(gd);
return count+1;
}
void frequency(double grades[], int numGrades){
    int j, i, a=0, b=a+4;
    for(j=0; j<numGrades-1; j++){
        printf("%d - %d|", a, b);
        for(i=0; i<numGrades; i++){
            if(grades[i]<=b && grades[i]>=a){
                printf("*");
        }
    }
    printf("\n");
    a+=5;
    if(a==100){
        break;
    }
}
printf("%d|", a);
for(j=0; j<numGrades; j++){
    if(grades[i]==100)
        printf("*");
}
printf("\n");
}

int maximum(double grades[], int numGrades){
    int i, h = 0;
    for(i=1; i<numGrades; i++){
    /*if(grades[i] == grades[h])
        continue;*/
    if(grades[i] > grades[h])
        h = i;
}
return h;
}

int minimum(double grades[], int numGrades){
    int i, h = 0;
    for(i=1; i<numGrades; i++){
    /*if(grades[i] == grades[h])
        continue;*/
    if(grades[i] < grades[h])
        h = i;
}
    return h;
}
int deleteElement(double grades[], int numGrades, int loc){
    int i;
    for(i=loc; i<numGrades-1; i++){
    grades[i] = grades[i+1];
    }
    return numGrades-=1;
}
double mean(double grades[], int numGrades){
    int i;
    double ans, sum;
    for(i = 0; i<numGrades; i++){
    sum += grades[i];
}
    ans = sum/numGrades;
    return ans;
}
double standardDeviation(double grades[], int numGrades){
    int i;
    double avg, sd, ans;
    avg = mean(grades, numGrades);
    for(i=0; i<numGrades; i++){
    sd += pow((avg - grades[i]), 2);
}
    ans = sqrt(sd/numGrades);
    return ans;
}
#包括
#包括
#包括
内部评级(双);;
无效频率(双[],整数);
整数最大值(双[],整数);
最小整数(双[],整数);
int deletelement(双[],int,int);
双平均值(双[],整数);
双标准偏差(双[],整数);
#定义最大值100
双级[最高];
int loc;
int main(){
国际努姆格拉德斯;
printf(“%lf”,等级);
numGrades=readGrades(等级);
loc=最小值(等级、等级);
numGrades=删除元素(等级、numGrades、loc);
printf(“数据已通过删除最小%.2lf\n”,等级[loc])进行了调整;
loc=最大值(等级、等级);
numGrades=删除元素(等级、numGrades、loc);
printf(“数据已通过删除最大%.2lf\n”,等级[loc])进行了调整;
printf(“调整后的平均值为%.2lf\n”,平均值(等级、数值));
printf(“调整后的标准偏差为%.2lf\n”,标准偏差(等级、数值));
printf(“这是调整后数据的柱状图:\n”);
频率(等级、等级);
返回0;
}
整数可读等级(双等级[]){
文件*gd;
整数计数=0;
双ind;
gd=fopen(“sample.txt”,“r”);
同时(计数<最大值){
fscanf(gd、%lf、&ind);
//双原子;
if(ind<0)
打破
等级[计数]=ind;
计数++;
}
/*而(fscanf(gd,%lf,&ind)>0){
等级[计数]=ind;
计数++;
}*/
/*做{
fscanf(gd、%lf)和等级[计数];
printf(“%.0lf”,等级[计数]);
如果(fscanf(gd,%lf,&等级[计数])=-999){
打破
}
计数++;
}而(fscanf(gd,%lf),&grades[count])>0*/
printf(“%.0lf”,等级[1]);
fclose(gd);
返回计数+1;
}
无效频率(双倍坡度[],国际标准){
int j,i,a=0,b=a+4;

对于(j=0;j检查
fopen
fscanf
调用的结果:

gd = fopen( "sample.txt", "r" );
if ( !gd )
{
  fprintf( stderr, "error opening input file\n" );
  return 0;
}

while( count < max && fscanf( gd, "%lf", &ind ) == 1 )
{
  if ( ind < 0 )
    break;
  grades[count++] = ind;
}

if ( feof( gd ))
  fprintf( stderr, "hit end of file\n" );
else if ( ferror( gd ))
  fprintf( stderr, "error during read\n" );
gd=fopen(“sample.txt”、“r”);
如果(!gd)
{
fprintf(stderr,“打开输入文件时出错”);
返回0;
}
而(计数
程序非常完美。我确信printf中存在问题,即您使用它的方式。您可以将打印值的行粘贴到其中。我希望您一定是这样做的

printf("%lf\n",grades[i]);
出于更好的原因,您甚至不需要将grades[]作为参数传递,因为这是一个全局变量。仅供参考,在readGrades()中,当您作为引用传递时,它会创建一个名为grades[]的局部变量。

另外,在readGrades()函数中,“return count”应该是可以的,因为您已经在while循环本身中增加了count。

正如@Pawan所说,您的代码完全可以。第二行
printf(“%lf”s,grades);
在main中是不需要的,并且在错误中运行(对我来说)。如果您在同一个“EXE”中有sample.txt路径不会有任何问题。如果sample.txt位于另一个文件夹中,则必须使用完整路径引用它。 这是电流输出:

循环中的Printint值:

65
99
87.000000
76.000000
89000000
37.000000
99
已通过删除最小值0.00来调整数据
通过删除最大值87.00对数据进行了调整
调整后的平均值为70.80
调整后的标准偏差为18.96
以下是调整后数据的柱状图:
0-4 |
5-4 |
10-4 |
15-4 |
20 |

如果无法打开该文件,请尝试

if ( !gd ) {
    fprintf( stderr, "error opening input file\n", strerror(errno));
    return 0;
}

查看错误。我认为另一个进程已打开该文件。

您试图在哪里打印值,即是在readGrades()函数内还是从调用方函数中打印值?请确保您正在readGrades()内访问相同的内存函数来填充调用方函数中声明的值。这就是我的意思;您如何看到数组中的所有零值?您必须执行类似printf()的操作来检查代码中的某个地方。该语句是在哪里编写的?它对我来说运行正常,所以您的错误可能在其他地方。请发布一个。您为什么返回“计数+1"?@Pottsy我们无法诊断您的问题,因为我们没有足够的代码;告诉我们您如何调用
readGrades
,然后如何检查
grades
的内容。我在while循环后尝试了上半部分nothing,它说打开文件时出错。我不明白它在第一次调用时如何不打开文件t line gd=fopen…这只会让我觉得我的IDE或计算机无法识别该文件。@Pottsy:输入文件是否与可执行文件位于同一目录中?如果不是,则必须指定文件路径(例如,Linux上的“/home/me/samples.txt”或Windows上的“C:\\home\\me\\samples.txt”)。你确定文件名正确吗?你有读取文件的权限吗?我将sample.txt int放在与.exe相同的位置,它的数据有点不可靠,但我可以调整它。
if ( !gd ) {
    fprintf( stderr, "error opening input file\n", strerror(errno));
    return 0;
}