###前提・実現したいこと
プログラミング初心者です。
cで画像ファイルの解析かつ判断するソフトの簡易版を作成しています。
どうも検証をしたところfreadがセグメンテーションに引っかかるらしく、原因がよくわかりません。どうか原因と解決策を教えていただけませんか?
追記)拙い英語でコメントを書いています。どうぞ温かい目で笑
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
int compare(unsigned int a,unsigned int b,int c) //approxicimation
{
int x,y;
x = a - c;
y = b - c;
if(x > y){
return 1;
} else{
return 0;
}
}
int main(int argc,char *argv[])
{
FILE *fpi;
struct dirent *dp;
DIR *dir1,*dir2; //file pointa
unsigned int average1=0,average2=0; //各画像の色彩データの平均値
int sample = 0;
int i=0,j=0;//the number of the file in the current direct
/* Command sentence checker */
if(argc != 4){
fprintf(stderr,"Usage: %s [input][input][output]\n",argv[0]);
exit(1);
}
if((dir1=opendir(argv[1])) == NULL){
fprintf(stderr,"input file open error\n");
exit(1);
}
if((dir2=opendir(argv[2])) == NULL){
fprintf(stderr,"input file open error\n");
exit(1);
}
if((fpi=fopen(argv[3], "rb")) == NULL){
fprintf(stderr,"input file open error\n");
exit(1);
}
/* materials file sort program */
do{
dp=readdir(dir1);
unsigned char idat=0;
fread(&idat,sizeof(unsigned char),1,(FILE *)dp);
average1 += idat;
i++;
}while(dp!=NULL);
average1 = average1/i; // the result of image sort printf("2\n");
for (dp=readdir(dir2);dp!=NULL;dp=readdir(dir2)){
unsigned char idat=0;
average2 += idat; j++;
}
average2 = average2/j; //the result of image sort
/* read of the sample */ unsigned char idat=0; fread(&idat,sizeof(unsigned char),1,fpi); sample = idat; /* compare between the sample and the result of the images study */ if(compare(average1,average2,sample)== 1){ puts("the image is similar with the former dirent"); } else{ puts("the image is similar with the latter dirent"); } closedir(dir1); closedir(dir2); fclose(fpi); return 0;
}
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報画像ファイル
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。