这是一头文件,查询数据
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
void Select()
{
int year,mon, day, hour, min;//定义要查询的时间
char str1[3];//必要的字符如: -
char typeName[5][6] = {"DHGD","TIME","DATE","STAT","DMGD"};//定义文件类型名字
char head_path[70] = "D:\\zuoye\\lzh2020031070\\lzh2020031070\\data";//文件上级目录的路径
char file_path[70];//文件路径名字,注意不要堆栈使数组越界
char time_date[70];//准确时间
int flag = 0;//文件存在标志,1为存在,0为不存在
char line_date[1024];//查找每行的数据
int tmper;//定义温度
char str_temper[6];
char show_temper[6];//显示的温度格式
int j;
printf("请输入要查询DMGD数据的年月日时分:(eg:2015-01-01 00:03)");
scanf("%d%c%d%c%d %d%c%d",&year,&str1[0],&mon,&str1[1],&day,&hour,&str1[2],&min);
sprintf(file_path,"%s\\DMGD\\DMGD_%d%02d%02d.txt",head_path,year,mon,day);
sprintf(time_date,"%02d:%02d",hour,min);
if (access(file_path, 0) == 0)//文件查询
{
//**----------文件查询和寻找温度——---------**/
FILE *fp = fopen(file_path,"r");
if (fp == NULL)
{
printf("文件打开失败");
exit(0);
}
while(fgets(line_date,1024,fp) != NULL)//将每行数据写入line_date
{
if (strstr(line_date,time_date) != NULL)//判断时间是在该行
{
int i = 0;
flag = 1;
for (i = 0; line_date[i] != '*'; i++);
str_temper[4] = line_date[i-2];
str_temper[3] = '.';
str_temper[2] = line_date[i-3];
str_temper[1] = line_date[i-4];
str_temper[0] = line_date[i-5];
str_temper[5] = '\0';
break;
}
}
/*---------处理温度的格式-----------*/
if (str_temper[2] == '-')//当温度为 -2 这种
{
str_temper[0] = '0';
str_temper[1] = '.';
str_temper[2] = str_temper[4];
str_temper[3] = '\0';
}
for (j = 0; str_temper[j] != '\0'; j++)
{
if (str_temper[j] == ' ')//当温度不是4位时
{
int i;
for (i = 0;str_temper[j] != '\0'; i++)
{
j++;
show_temper[i] = str_temper[j];
}
}
else
{
strcpy(show_temper,str_temper);
}
}
}
/*------显示---------------*/
if(flag == 1)
{
printf("\n查询到温度是%s℃", show_temper);
}
else
{
printf("\n要查询的数据缺失");
}
printf("\n自动站数据查询完毕");
system("pause");
}
在运行的时候,显示堆栈了,但增加了line_data和head_path的值,还是会有错误