繁体   English   中英

输出到文件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
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM