你试试在VC6上修改编译:
程序代码:
程序代码:
#include <cstdio>
#include <cstring>
#include <vector>
struct _Data
{
char Time[10]; // 时间
short Latitude; // 纬度
short Longitude; // 经度
double Value; // 值
};
std::vector<std::string> Time_List; // 时间清单
std::vector<short> Latitude_List; // 纬度清单
std::vector<short> Longitude_List; // 经度清单
std::vector<_Data> Data_List; // 数据表
void Load_Data(const char* time);
void Get_LatitudeList(FILE* file);
void Get_LongitudeList(FILE* file);
void Get_DataList(FILE* file, const char* time);
void Save_DataList(const char* filename);
double GetData(short longitude, short latitude, const char* time);
void main(int argc, char* argv[])
{
for (int index = 1; index < argc; ++index)
{
char time[10];
strcpy_s(time, sizeof(time), argv[index]);
Time_List.push_back(std::string(time));
Load_Data(time);
}
Save_DataList("2011.TXT");
}
// 读入指定文件的数据,例如2011.01.1.TXT,传入参数2011.01.1,自动补全扩展名为2011.01.1.TXT
void Load_Data(const char* time)
{
char file_name[FILENAME_MAX];
strcpy_s(file_name, sizeof(file_name), time);
strcat_s(file_name, sizeof(file_name), ".TXT");
FILE* source_file;
errno_t errorCode = fopen_s(&source_file, file_name, "rt");
if (Latitude_List.empty())
{
Get_LatitudeList(source_file);
}
if (Longitude_List.empty())
{
Get_LongitudeList(source_file);
}
Get_DataList(source_file, time);
fclose(source_file);
}
// 读入纬度清单
void Get_LatitudeList(FILE* file)
{
char buffer[1024];
char seps[] = "\t";
char* token;
char* next_token;
fseek(file, 0L, SEEK_SET);
fgets(buffer, sizeof(buffer), file);
token = strtok_s(buffer, seps, &next_token);
while (token != NULL)
{
Latitude_List.push_back(atoi(token));
token = strtok_s(NULL, seps, &next_token);
}
}
// 读入经度清单
void Get_LongitudeList(FILE* file)
{
char buffer[1024];
fseek(file, 0L, SEEK_SET);
fgets(buffer, sizeof(buffer), file);
while (!feof(file))
{
if (fgets(buffer, sizeof(buffer), file) != NULL)
{
char seps[] = "\t";
char* token;
char* next_token;
token = strtok_s(buffer, seps, &next_token);
Longitude_List.push_back(atoi(token));
}
}
}
// 读入数据
void Get_DataList(FILE* file, const char* time)
{
char buffer[1024];
fseek(file, 0L, SEEK_SET);
fgets(buffer, sizeof(buffer), file);
int row = 0;
while (!feof(file))
{
if (fgets(buffer, sizeof(buffer), file) != NULL)
{
++row;
char seps[] = "\t";
char* token;
char* next_token;
int col = 0;
token = strtok_s(buffer, seps, &next_token);
while (token != NULL)
{
if (col > 0)
{
_Data d;
strcpy_s(d.Time, sizeof(d.Time), time);
d.Latitude = Latitude_List[col - 1];
d.Longitude = Longitude_List[row - 1];
d.Value = atof(token);
Data_List.push_back(d);
}
token = strtok_s(NULL, seps, &next_token);
++col;
}
}
}
}
// 另存数据到指定文本文件
void Save_DataList(const char* filename)
{
FILE* file;
errno_t errorCode = fopen_s(&file, filename, "wt");
if (errorCode != 0)
{
printf_s("File %s create failure!\n", filename);
}
fprintf_s(file, "经度\t纬度");
for (int index = 0; index != Time_List.size(); ++index)
{
fprintf_s(file, "\t%s", Time_List[index].c_str());
}
fprintf_s(file, "\n");
for (int longitude_index = 0; longitude_index != Longitude_List.size(); ++longitude_index)
{
for (int latitude_index = 0; latitude_index != Latitude_List.size(); ++latitude_index)
{
fprintf_s(file, "%d\t%d", Longitude_List[longitude_index], Latitude_List[latitude_index]);
for (int time_index = 0; time_index != Time_List.size(); ++time_index)
{
fprintf_s(file, "\t%10.2f", GetData(Longitude_List[longitude_index], Latitude_List[latitude_index], Time_List[time_index].c_str()));
}
fprintf_s(file, "\n");
}
}
fclose(file);
printf_s("Output file %s created!\n", filename);
char command[FILENAME_MAX];
sprintf_s(command, "NOTEPAD %s", filename);
system(command);
}
// 根据经度、纬度和时间在数据表中取数据值
double GetData(short longitude, short latitude, const char* time)
{
for (std::vector<_Data>::const_iterator index = Data_List.cbegin(); index != Data_List.cend(); ++index)
{
if ((index->Longitude == longitude) && (index->Latitude == latitude) && (strcmp(index->Time, time) == 0))
{
return index->Value;
}
}
return 0;
}

授人以渔,不授人以鱼。







