输出到文件c编程

output to file c programming

我有一个输出数组,这些输出是在一个模型中生成的,其中有一个链接到它的源代码文件。此处引用为

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
    ...
}