私は、データファイルを読み込む機能を持つ大きなプロジェクトに取り組んでいます。ただし、特定のテストコードでは、そのファイルは存在しないため、作成時に空のテキストファイルが作成されます。私は、このイベントを補うために、次のコードを書いた:空のファイルを読み取っているときにfscanfがクラッシュする
typedef struct system_boot_status_s{
char timestamp[18];
int power_down_type;
int power_down_cause;
int boot_number;
int antenna_deployed;
int images_captured;
int beacon_count;
}system_boot_status_t;
////////////////////////////////
// Read the boot status info into the boot status struct
ret = fscanf(f, "%s %d %d %d %d %d %d",
bootstatus->timestamp,
bootstatus->power_down_type,
bootstatus->power_down_cause,
bootstatus->boot_number,
bootstatus->antenna_deployed,
bootstatus->images_captured,
bootstatus->beacon_count);
if (ret != 7) // if 7 items weren't read
{
// Make sure all boot status members are set to 0
snprintf(bootstatus->timestamp, BOOT_INFO_LEN, "xx-xx-xx-xx-xx-xx");
bootstatus->power_down_type = 0;
bootstatus->power_down_cause = 0;
bootstatus->boot_number = 0;
bootstatus->antenna_deployed = 0;
bootstatus->images_captured = 0;
bootstatus->beacon_count = 0;
return -1;
}
私は関数fscanfは、読み込むものの数を返すことを知っているが、私はこのプログラムを実行し、それが空のファイルに達したときに、私のプログラムだけでフリーズします。私はEOFでやっていなければならないことが何か不足していますか?誰か助けてくれますか?
「power_down_type」などのこれらの変数にはどのようなタイプがありますか? 'int'? 'int *'? –
ファイルが正しく開くことをどのように知っていますか? '&' address-of演算子が間違っていて、 'struct'にポインタが含まれていることをどうやって知っていますか?問題を示す[最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)を投稿してください。 2番目のブロックでは、あなたはポインタではないことを示唆する 'bootstatus-> power_down_type = 0;'を持っています。 –
typedef struct system_boot_status_s { char timestamp [18]; int power_down_type; int power_down_cause; int boot_number; int antenna_deployed; int images_captured; int beacon_count; } system_boot_status_t; –