简体   繁体   English

如何从C中的文件中保存我在数组中读取的数据

[英]How to save the data which i read in an array from a file in C

So while doing this assignment i encountered a problem where i tried to save some set of values(float) in an Array so that i can use them later on producing a graph, but the problem which i face here is that i read the values and i can print them but later which i checked the array the numbers which were stored there were not the same. 所以在做这个任务的时候我遇到了一个问题,我试图在一个数组中保存一些值(浮点数),以便我可以在以后生成图形时使用它们,但我在这里遇到的问题是我读取的值和我可以打印它们,但后来我检查了数组,存储的数字不一样。

Im trying to save in in avg[]. 我试图在avg []中保存。

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


float maximum(float array[])
{
float max=0;
for(int i=0;i<100;i++)
{
    if (array[i]>max)
        max=array[i];
}
return max;
}

float minimum(float array[])
{
float min=0;
for(int i=0;i<100;i++)
{
    if (array[i]<min)
        min=array[i];
}
return min;
}





char * read(char *filename)    
 {
FILE * sample;
sample = fopen(filename,"r");  //like file open in notepad  ,which file? and    what to do?
int count = 0;
static char singleline[100];  
int cnt = 0;
int sum = 0;
int oldyear = -1;
float avg[82];

while(!feof(sample))    //read that until end.
{
    count++;            
    fgets(singleline,150,sample);

    if (count>21 && singleline[33]!='9') 

    {               
        char y[5], t[6];
        for (int i = 14; i < 18; i++)
        {
            y[i - 14] = singleline[i];
        }
        y[4]='\0';
        for (int i= 24;i <29;i++)
        {
            t[i-24]=singleline[i];
        }

        t[5]='\0';
        int year = atoi(y);
        int temp = atoi(t);
        //printf("year : %i ,temp: %i\n",year, temp);

        if (year == oldyear)
        {
            cnt++;
            sum += temp;
        }
        else 
        {   
            int l=0;
            l++;
            avg[l] = 0.1 * sum / cnt;
            if (cnt != 0) 
            {       
                printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\n", oldyear, cnt, avg[l]);
                cnt = 1;
                sum = temp;
                //save[l]=avg;
            }
            oldyear = year;     
        }

    }

}               
            float last = 0.1 * sum / cnt;
            printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\n", oldyear, cnt-1, last);
            fclose(sample);
            //for(int i=0;i<)


for(int i=0;i<125;i++)
{
 printf("%f\n",avg[i]);
}       
printf("\nMax Temp= %f",maximum(avg));
printf("\nMax Temp= %f",minimum(avg));
return singleline;

 }


 int main()
 {  

char * file1 = "TG_STAID000476.txt";
read(file1);

//char * file2 = "TG_STAID000179.txt";
//read(file2);


return 0;
}

So yea, the problem was to read the year and the corresponding values and form an Average value for that particular year and then represent it in a graph. 所以,问题是阅读年份和相应的值,并形成该特定年份的平均值,然后在图表中表示。

I can do the first part where it takes the Average, but when i tried using it later,it had wrong values stored in it, you can see that where i tried to print all the values of avg[], can anyne please help me figure out how to correct the mistake, i want them to be saved as float. 我可以做第一部分,它需要平均值,但是当我以后尝试使用它时,它存储了错误的值,你可以看到我试图打印avg []的所有值,可以任何请帮助我弄清楚如何纠正错误,我希望他们被保存为浮动。

The assignment datasheet is here. 分配数据表在这里。 https://www.scribd.com/document/333844245/TG-STAID000179 https://www.scribd.com/document/333844245/TG-STAID000179

I tried reading the values and used atoi to save them, and then used them to get the Average, i used Count>21 because the text before them are not required and when it reads a '9' on 34th column,it ignores since its not a valid data(in data sheet) 我尝试读取值并使用atoi来保存它们,然后使用它们来获得平均值,我使用Count> 21因为它们之前的文本不是必需的,当它在第34列读取'9'时它会忽略,因为它不是有效数据(在数据表中)

  1. The variable l , intended to count the computed year temperature averages, is defined and initialized in the block where a year change is handled, thus always reset to zero. 用于计算计算年温度平均值的变量l在处理年变化的块中被定义和初始化,因此总是重置为零。 Correct this by moving the definition outside of the line read loop, perhaps even at global scope (see 2.). 通过将行定义移到行读取循环之外来校正这一点,甚至可能在全局范围内(参见2.)。
  2. The maximum() and minimum() functions operate on array elements 0 to 99, irrespective of the fact that the passed argument is defined as avg[82] and some elements at the end are not initialized. maximum()minimum()函数对array元素0到99进行操作,而不管传递的参数是否定义为avg[82]并且末尾的某些元素未初始化。 Correct this by using the actual number l of stored elements, either as a global scope variable or an additional function argument. 通过使用存储元素的实际数量l来纠正此问题,可以是全局范围变量或附加函数参数。
  3. The singleline buffer is too short and thus overflows. singleline缓冲区太短,因此溢出。
  4. The while(!feof(sample)) loop cycles one extra time after the last line has been read, because EOF is not recognized before the next fgets() . while(!feof(sample))循环在读取最后一行后再循环一次,因为在下一个fgets()之前无法识别EOF。 Correct this by using the return value of fgets() to test EOF. 通过使用fgets()的返回值来测试EOF来纠正此问题。
  5. avg[l] = 0.1 * sum / cnt is evaluated even if cnt is zero. 即使cnt为零,也评估avg[l] = 0.1 * sum / cnt Correct this by moving this expression inside the if (cnt != 0) { … } block, also the l++ , but behind the printf() . 通过将此表达式移动到if (cnt != 0) { … }块(也是l++ ,但在printf()后面来纠正此问题。
  6. cnt = 1 is not executed if cnt is zero. 如果cnt为零,则不执行cnt = 1 This causes the very first data point to not be counted. 这会导致第一个数据点无法计数。 Correct this by moving this expression behind the if (cnt != 0) { … } block. 通过将此表达式移到if (cnt != 0) { … }块后面来纠正此问题。
  7. The extra loop cycle (cf. 4.) may have led to use cnt-1 in the final printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\\n", oldyear, cnt-1, last); 额外的循环周期(参见4.)可能导致在最终的printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\\n", oldyear, cnt-1, last);使用cnt-1 printf("Fuer %i - %i Werte - Durchschnitt %.2f °C\\n", oldyear, cnt-1, last); , but correct is just cnt . ,但正确的只是cnt
  8. The loop for(int i=0;i<125;i++) should also use the actual number l of stored elements, not 125 . for(int i=0;i<125;i++)的循环也应该使用存储元素的实际数量l ,而不是125
  9. To be noted is that the final year's average is (maybe intentionally) not stored in avg[] . 需要注意的是,最终年份的平均值(可能是有意的)没有存储在avg[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM