使用数组来读取文件 [英] Using an array to read a file

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

问题描述

我所试图做的就是打开一个文件,读取信息的文件中,要求用户提供一系列的数据,并计算天各月落入该范围的百分比。该文件是每个月中的每一天的气象数据(温度)的文件,有几年的过程。我有难同一个阵列启动程序,我不知道如何使用数组来从文件中的信息,然后计算和存储,我需要的信息。任何帮助是AP preciated,这里是我的code迄今:

 的#include<&stdio.h中GT;诠释主(){
    INT范围[12],总[12],我,冷,热,总之,输入,月,日,年;
    浮动平均,温度;
    字符文件名[20];    的printf(请告诉我你的preferred下飞行:\\ n);
    的printf(什么是你可以在飞行的最低温度\\ n吗?);
    scanf函数(%d个,&安培;冷);    的printf(什么是你可以在飞行的最高温度\\ n吗?);
    scanf函数(%d个,&安培;热);    FILE * IFP = 0;
    的printf(请输入气象数据文件的名称\\ n);
    scanf函数(%S,文件名);
    IFP = FOPEN(文件名,R);    返回0;
}

现在我只是有年初完成,我认为是正确的,但我不知道下次什么用数组做才能得到信息的文本文件。我不需要整个程序完成后,我只需要在下一步做什么帮助。这里的输出应该是什么样子的例子:

 说说你​​的preferred下飞行:
什么是你可以在飞行的最低温度?
60什么是你可以在飞行中最热的温度是多少?
80请输入的气象数据的文件的名称。
的weather.txt1个月:在范围内的天59.2个百分点。
本月2:日期范围69.2个百分点。
3个月:在范围内的天72.6个百分点。
4个月:在范围内的天92.6个百分点。
本月5:日期范围的98.7%。
本月6日:在范围内的天48.3%。
本月7:日期范围36.41个百分点。
8月:在范围内的天18.9%。
9月:在范围内的天57.64个百分点。
本月10:日期范围100.00个百分点。
本月11:日期范围65.4个百分点。
本月12日:在范围内的天80.5个百分点。我建议每月10飞行!

编辑:
该文本文件的格式如下:

 可用的数据是每天四列psented $ P $:
月日年温度
该文件将与本身的一排-1结束。

从文件示例:

  1 1 2013 63.4
 1 2 2013 66.1
 1 3 2013 67.2


解决方案

使用 与fgets 你打开后,从文件中读取 -

  char数据[100];
而(与fgets(数据,sizeof的数据,IFP)!= NULL){
    //获取温度超出数组
    //将其转换并存储在一个变量
}

或者,如果在文件中的数据是固定的格式使用 的fscanf

注意的 - 的fopen 的检查收益值。

What I am trying to do is open a file, read the information in the file, ask the user for a range of data, and calculate the percentage of days in each month that fall into that range. The file is a file with weather data (temperature) for each day of each month, over a course of years. I am having difficulty starting the program with an array and I am not sure how to use the array to get the information from the file, and then calculate and store the information that I need. Any help is appreciated, here's my code so far:

#include <stdio.h>

int main() {
    int range[12], total[12], i, cold, hot, sum, input, month, day, year;
    float avg, temp;
    char filename[20];

    printf("Tell me about your preferred temperature for flying:\n");
    printf("What is the coldest temperature you can fly in?\n");
    scanf("%d", &cold);

    printf("What is the hottest temperature you can fly in?\n");
    scanf("%d", &hot);

    FILE * ifp = 0;
    printf("Please enter the name of the weather data file.\n");
    scanf("%s", filename);
    ifp = fopen(filename, "r");

    return 0;
}

Right now I just have the beginning finished and what I think is correct, but I am not sure what to do next with the array in order to get the information out of the text file. I don't need the entire program finished, I just need help on what to do next. Here's an example of what the output should look like:

Tell me about your preferred temperature for flying:
What is the coldest temperature you can fly in?
60

What is the hottest temperature you can fly in?
80

Please enter the name of the weather data file.
weather.txt

Month 1: 59.2 percent of days in range.
Month 2: 69.2 percent of days in range.
Month 3: 72.6 percent of days in range.
Month 4: 92.6 percent of days in range.
Month 5: 98.7 percent of days in range.
Month 6: 48.3 percent of days in range.
Month 7: 36.41 percent of days in range.
Month 8: 18.9 percent of days in range.
Month 9: 57.64 percent of days in range.
Month 10: 100.00 percent of days in range.
Month 11: 65.4 percent of days in range.
Month 12: 80.5 percent of days in range.

I recommend month 10 for the flight!

Edit: The text file is in this format:

The available data is presented in four columns per day:
MONTH         DAY         YEAR         TEMPERATURE
The file will end with a -1 on a row by itself. 

Example from the file:

 1             1             2013         63.4
 1             2             2013         66.1
 1             3             2013         67.2

解决方案

Use fgets to read from file after you open it -

char data[100];
while(fgets(data,sizeof data,ifp)!=NULL){
    // get temperature out of array 
    // convert it and store in a variable 
}

Or if data in file is in fixed format use fscanf .

Note - Check return value of fopen .

这篇关于使用数组来读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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