2015-03-30 36 views
0

我有一個模型中生成的輸出數組以及鏈接到它的源代碼文件。它在這裏被引用爲輸出到文件c編程

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"); 
    } 
+0

以及在功能wheere生成輸出,該輸出struct被調用,像這樣'struct nrlmsise_output output [ARRAYLENGTH];''for(i = 0; i spacedancechimp 2015-03-30 18:58:19

+1

請包括'struct nrlmsise_output'的定義 – 2015-03-30 19:00:49

+1

是的,包括結構定義,並編輯您的帖子,而不是添加新的代碼到評論 – mstbaum 2015-03-30 19:01:47

回答

1

output都沒有看到它們被聲明和使用功能之外你的局部變量。除了具有相同的名稱外,兩個函數中的變量output都是不相關的:它們不保存相同的數據。

您需要任一聲明output作爲全局陣列或數組傳遞給test_gtd7()

void test_gtd7(struct nrlmsise_output *output) { 
    ... 
} 

void outfunc(void) { 
    struct nrlmsise_output output[ARRAYLENGTH]; 
    ... 
    test_gtd7(&output); 
    ... 
} 

OR

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

謝謝!第二個選項奏效。 – spacedancechimp 2015-03-30 19:29:01

+0

在第一個選項中,您可能需要編輯訪問作爲參數傳遞的數組的方式,mybad我沒有深入其中。 – 2015-03-30 19:30:36

+0

哦,不,我有這個功能的其他結構打電話給我,只是把它們留在這裏,這就是爲什麼我認爲哈哈。 – spacedancechimp 2015-03-30 19:32:39