以下是引用wo严在2015-8-27 16:11:38的发言:
我这里51和341都是确定的,因为这是一个分析软件,设定的就是51和341,所以得到的结果里面这两个数不变的
我这里51和341都是确定的,因为这是一个分析软件,设定的就是51和341,所以得到的结果里面这两个数不变的
那可以直接在全局数据区中分配出固定的二维数组,然后参照我Load_Data()函数中的手法把数据读进去。

授人以渔,不授人以鱼。

2015-08-27 16:18
能不能麻烦您把这个直接确定51和341个情况下读取数据的这段程序给我写一下,我现在对这块比较糊涂,还没有弄明白!
2015-08-27 20:04
2015-08-27 20:15
2015-08-27 20:36
2015-08-27 20:39
2015-08-28 10:28
2015-08-28 10:46
2015-08-28 10:51
2015-08-28 16:28
程序代码:
/*
* 我想读入附件中的数据,我想读入Y对应的数据,并将数据存入到a[i][j]中去,其中i表示第i个x,j表示第j个curve;
* 例如对应的数据为a[0][0]=3.500000e+002, a[1][0]=3.487281e+002直到把全部Y数据存入到a[i][j]这个50*341二维数组中!
*/
#pragma comment( compiler )
#include <cstdio>
#include <cstdlib>
#include <string>
#include <conio.h>
const size_t PointsPerCurve = 51; // 每幅图的点数
const size_t CurveNumber = 341; // 图的数目
double Points[PointsPerCurve][CurveNumber]; // 点阵数据
void Pause(void)
{
printf_s("\nPress any key to continue...");
_getch();
}
bool Load_Data(const char* fileName)
{
FILE* inputFile;
errno_t errorCode = fopen_s(&inputFile, fileName, "rt");
if (errorCode != 0)
{
printf_s("数据源文件%s打开错误,错误码为%d,请在网上检索错误码对应的信息。\n", fileName, errorCode);
Pause();
return false;
}
char bufferLine[1024];
size_t curveNo = 0;
while (fgets(bufferLine, _countof(bufferLine), inputFile) != NULL)
{
bool newCurve = false;
if (strstr(bufferLine, "Curve") != NULL)
{
++curveNo;
}
if (strstr(bufferLine, "------------------------------") != NULL)
{
newCurve = true;
}
if (newCurve)
{
for (size_t pointNo = 0; pointNo < PointsPerCurve; ++pointNo)
{
struct Point
{
double Increment; // X
double Temperature; // Y
} point;
if (fscanf_s(inputFile, "%lf %lf\n", &point.Increment, &point.Temperature) == 2)
{
Points[pointNo][curveNo - 1] = point.Temperature;
}
}
newCurve = false;
}
}
fclose(inputFile);
return true;
}
void Save_Data(const char* fileName)
{
FILE* outputFile;
fopen_s(&outputFile, fileName, "wt");
for (size_t pointNo = 0; pointNo < PointsPerCurve; ++pointNo)
{
for (size_t curveNo = 0; curveNo < CurveNumber; ++curveNo)
{
fprintf_s(outputFile, "%e ", Points[pointNo][curveNo]);
}
fputc('\n', outputFile);
}
fclose(outputFile);
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf_s("请以数据源文件名为参数启动程序\n");
Pause();
return EXIT_SUCCESS;
}
if (Load_Data(argv[1]))
{
// 输出数据到文件中以便验证正确性
Save_Data("opt_out.txt");
}
return EXIT_SUCCESS;
}

2015-08-28 18:07