1
我正在寫一個代碼來存儲來自文本文件的數據。代碼編譯,但它似乎停止運行在ReadPanelBlock
函數for循環。它的結構爲: CHBStructure
包含CHBPanels
其中包含CHBOpenings
。這些功能是爲了從一個輸入文本文件中讀取而創建的,我將這些文件組織成數據塊以便於閱讀。結構體處理和結構體陣列
typedef struct _open
{
int id;
double length;
double height;
double origX;
double origY;
int frames;
double thickness;
double E;
double v;
}CHBOpening;
typedef struct _panels
{
int id;
double length;
double height;
double origX;
double origY;
double origZ;
double angle;
int nOpenings;
int nReinforcement;
double *xReinf;
double sx;
double xReinf0;
CHBUnit *chb;
CHBOpening *openings[];
}CHBPanel;
typedef struct _chb
{
int nStories;
int nModes;
int nIter;
int nPanels;
CHBPanel *panels[];
}CHBStructure;
int ReadPanelBlock (FILE *fp, CHBStructure *S)
{
CHBOpening *openings = malloc(sizeof(CHBOpening));
*S->panels = malloc(S->nPanels*sizeof(CHBPanel));
for (i=0; i<S->nPanels; i++)
{
fscanf(fp,"%d",&S->panels[i]->id);
fscanf(fp,"%lf",&S->panels[i]->length);
fscanf(fp,"%lf",&S->panels[i]->height);
fscanf(fp,"%lf",&S->panels[i]->angle);
fscanf(fp,"%lf",&S->panels[i]->origX);
fscanf(fp,"%lf",&S->panels[i]->origY);
fscanf(fp,"%lf",&S->panels[i]->origZ);
fscanf(fp,"%d",&S->panels[i]->nOpenings);
if (S->panels[i]->nOpenings > 0)
{
for (j=0; j<S->panels[i]->nOpenings;j++)
{
openings = S->panels[i]->openings[j];
fscanf(fp,"%d",&openings->id);
fscanf(fp,"%lf",&openings->length);
fscanf(fp,"%lf",&openings->height);
fscanf(fp,"%lf",&openings->origX);
fscanf(fp,"%lf",&openings->origY);
}
}
}
return 1;
}
EDIT 1:我嘗試跟蹤在由每行打印文本中發生錯誤,它似乎在ReadPanelBlock功能的第一fscanf函數崩潰。
我試圖插入之前剛內malloc的線,你建議,但現在報告無效使用可變數組成員。 –
這有助於很多!語法上沒有問題,但程序仍然崩潰。我做了一些檢查,似乎文件指針不再讀取文件中的值,因此結構開放的成員的值爲0.0。該程序也不會返回到外部循環,並在內部循環的最後一次迭代中停止進程。有什麼想法嗎?再次感謝您的幫助! –
@CarlChesterRagudo首先猜測:'S> nPanels = 1',你必須檢查數據是如何組織到源文件中的。用您的代碼和文件內容樣本發佈一個新問題。 – LPs