C:从文本文件读入一个结构数组 [英] C: Read from textfile into a struct array

查看:87
本文介绍了C:从文本文件读入一个结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出的程序是应该从文本文件中的数字读取和保存号的总数,则数字在struct平均值

The program I am making is supposed to read in numbers from a text file and save the total number of numbers, the average value of the numbers in a struct.

我有一个结构,看起来像这样:

I have a struct that looks like this:

struct seriepost {
    int totnr;   
    int outnr;   
    float average;  
};

和函数(未完)看起来是这样的:

And the function (unfinished) looks like this:

int read_data(FILE *tsin, struct seriepost serie[]) {   

        int x = 0;
        float average = 0;
        float in_last = 0;
        while (!feof(tsin))
        {
            while (fscanf(tsin, "%f", &in_last) != 0.0)
            {
            serie[x].totnr += 1;
            serie[x].medel = average/serie[x].totnr;
            serie[x].outnr = average*1.05+average*0.95;
            }
            x += 1;
        }
        fclose(tsin);
        return sizeof(serie);
    }

该文本文件看起来像这样:

The text file looks like this:

22.2 12.4 24.5 12.4..... 
22.2 12.2 0.0

2.21 12.1 11.1 11.1.... 
1.1 0.0 

其中0.0标记的系列结束

Where 0.0 marks the end of a series.

现在我想,直到0.0,然后我希望它跳到下一个阵列点为下一个系列的fscanf读取所有的数字。所以我有一个像意甲[0],意甲[1]用自己的一组数字与平均数值等。

Now i want the fscanf to read all the numbers until 0.0 then i want it to skip to the next array spot for the next series. So i have like serie[0], serie[1] with their own set of numbers and averages values etc.

推荐答案

如果你看到的 的fscanf 参考,你会看到,它返回成功扫描的项目数,而不是你刚才扫描的价值。

If you see a reference of fscanf, you will see that it returns the number of items successfully scanned, and not the value you just scanned.

这意味着你必须检查不同的为您结束条件。

That means you have to check differently for your ending condition.

您还不能循环而(!的feof(...))作为档案结尾的情况将不会设置直到你已经尝试阅读超出文件的末尾。

You also should not loop while (!feof(...)), as the end-of-file condition will not be set until you already tried to read beyond the end of the file.

相反,你可以做这样的事情:

Instead you could do something like this:

while (fscanf(tsin, " %f", &in_last) > 0)
{
    if (in_last == 0.0f)
        ++x;
    else
    {
        /* The rest of your code */
    }
}

这会读,直到你达到最终的文件,或有错误,并增加索引,一旦你读 0.0 。在的fscanf 格式字符串中的前导空格让的fscanf 跳过空格(空格,制表符,换行符)。

This will read until you reach end-of-file, or there is an error, and increase the index once you read 0.0. The leading space in the fscanf format string make fscanf skip whitespace (spaces, tabs, newlines).

也有一对夫妇的其他问题与你的code。例如,您多纳特更新平均变量。 意甲值[X] .totnr 可能无法初始化,所以它增加(和一般使用它的操作)会在这种情况下是不确定的为好。然后使用的sizeof(系列)这是行不通的,因为系列实际上不是在功能的数组,但一个指针,因此 sizeof的运算符返回指针的大小,而不是阵列中的条目数。相反,返回 X 也就是现在的数组的大小。

There are also a couple of other problems with your code. For example, you donät update the average variable. The value of serie[x].totnr may not be initialized, and so increasing it (and using it in operations in general) will in that case be undefined as well. And then you use sizeof(series) which will not work, as series is not actually an array in the function but a pointer, so the sizeof operator returns the size of the pointer and not the number of entries in the array. Instead return x which is now the size of the array.

这篇关于C:从文本文件读入一个结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆