現在のディレクトリ内のファイルが Unix で一定時間後に変更されたかどうかを確認するプログラムを作成しています。私が使う:
struct tm* clock; // create a time structure
struct stat attrib; // create a file attribute structure
DIR * directory = NULL; // create directory file
directory = opendir(cwd);
if(directory == NULL)
return -1;
struct dirent * ent;
time_t tim=time(NULL); // acquire time information
struct tm * now=localtime(&tim);
後で私は持っています:
while((ent = readdir (directory)) != NULL)
{
stat(ent->d_name, &attrib); // get the attributes of file
clock = gmtime(&(attrib.st_mtime)); // Get the last modified time
{...//printf's to print all tm_ measurements and iyr,imonth,etc}
if(iyr<=clock->tm_year+1900 && imonth<=clock->tm_mon+1 && iday <= clock->tm_mday && ihour<=clock->tm_hour && imin <= clock ->tm_min)
{ ...}
不条理な量の条件を許して、なぜ clock->tm_mon はセグ フォールトを引き起こさないのに、clock->tm_mon+1 はセグ フォールト クラッシュを引き起こすのですか? 上記のコードでは、ループ内の 1 つの印刷コマンドの後に seg fault が発生しました。整数 b を作成し、それを clock->tm_mon に等しく設定し、1 つ追加して、clock->tm_mon+1 の代わりに使用すると、セグ フォールトが発生しますが、後でいくつか出力されます。
iyr、imonth などは、最近の年、月などを適切に表す整数であり、月の +1 を説明するために何をしようとしても、ループ内でセグ フォールトが発生することを確認しました。また、ループの残りの if 条件を通過するものは何もなく、if 条件の後は何も起こらないことも確認しました。