我有一个输出数组,这些输出是在一个模型中生成的,其中有一个链接到它的源代码文件
struct nrlmsise_output output[ARRAYLENGTH];
在我编写的以下函数中。我只是想把这些输出从另一个功能生成
output[i].d[5]
在一个文件中,以便我在Python程序中使用。我最终需要它成为Python中的csv文件,所以如果有人知道如何直接将其制作成.csv,那将是非常棒的,但我还没有找到一个成功的方法,所以.txt很好。到目前为止,当我运行代码和输出文件时,我得到了我想要的格式,但输出中的数字太离谱了。(当我使用10^-9时,值为10^-100)。有人能说出为什么会发生这种事吗?此外,我已经尝试将输出放在一个单独的数组中,然后从该数组调用,但没有成功。我可能做得不对,然而,这个项目是我第一次不得不使用C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "nrlmsise-00.h"
#define ARRAYLENGTH 10
#define ARRAYWIDTH 7
void test_gtd7(void) {
int i;
struct nrlmsise_output output[ARRAYLENGTH];
for (i=0;i<ARRAYLENGTH;i++)
gtd7(&input[i], &flags, &output[i]);
for (i=0;i<ARRAYLENGTH;i++) {
printf("nRHO ");
printf(" %2.3e",output[i].d[5]);
printf("n");
//The output prints accurately with this.
}
}
void outfunc(void){
FILE *fp;
int i;
struct nrlmsise_output output[ARRAYLENGTH]; //I may be calling the output wrong here
fp=fopen("testoutput.txt","w");
if(fp == NULL)
{
printf("There is no such file as testoutput.txt");
}
fprintf(fp,"RHO");
fprintf(fp,"n");
for (i=0;i<ARRAYLENGTH;i++) {
fprintf(fp, "%E", output[i].d[5]);
fprintf(fp,"n");
}
fclose(fp);
printf("n%s file created","testoutput.txt");
printf("n");
}
在声明和使用局部变量output
的函数之外,不会看到它们。两个函数中的每个函数中的变量output
除了具有相同的名称之外都是不相关的:它们不包含相同的数据。
您需要将output
声明为全局数组,或者将数组传递给test_gtd7()
void test_gtd7(struct nrlmsise_output *output) {
...
}
void outfunc(void) {
struct nrlmsise_output output[ARRAYLENGTH];
...
test_gtd7(&output);
...
}
或
struct nrlmsise_output output[ARRAYLENGTH]; // gobal array
void test_gtd7() {
//struct nrlmsise_output output[ARRAYLENGTH]; // remove
...
}
void outfunc(void) {
//struct nrlmsise_output output[ARRAYLENGTH]; // remove
...
}