fgetcで読み取ったものを2次元配列へ格納したい
Posted: 2015年6月18日(木) 14:44
テキストファイルを2次元配列へいれたいです。
今やっているやり方は、一度fgetcで1次配列へ格納し、
それを2次元配列へいれる。感じでしています。
なぜ、fgetsを使わないかというと、crの改行も扱いたいからです。
下に入力処理の関数のコードを載せました。
これで行うと、
*** stack smashing detected ***: ./a.out terminated
となり、エラーになります。
fgetsで読み取り格納したように、fgetcを使う方法を教えてください。
今やっているやり方は、一度fgetcで1次配列へ格納し、
それを2次元配列へいれる。感じでしています。
なぜ、fgetsを使わないかというと、crの改行も扱いたいからです。
下に入力処理の関数のコードを載せました。
これで行うと、
*** stack smashing detected ***: ./a.out terminated
となり、エラーになります。
fgetsで読み取り格納したように、fgetcを使う方法を教えてください。
int input(void)
{
int word_cnt, loop_word, cnt;
FILE *fp;
char getc_word[500];
char before_date[100][50];
char get_ch;
fp = fopen(hk.in_file,"r");
if ( fp == NULL ) {
printf("指定された入力ファイルが存在しないか、開けません\n");
exit (-1);
}
printf("1次元配列へ\n");
for(word_cnt=0; (get_ch = fgetc(fp)) != EOF; word_cnt++) {
getc_word[word_cnt] = get_ch;
if (getc_word[word_cnt] == 0x0a){
//改行文字を終端文字に置き換える
getc_word[word_cnt] = '\0';
}
if (getc_word[word_cnt] == 0x0d){
//改行文字を終端文字に置き換える
getc_word[word_cnt] = '\0';
}
getc_word[word_cnt] = toupper(getc_word[word_cnt]);
}
loop_word = 0;
for(cnt = 0; loop_word < word_cnt; cnt++){
while(getc_word[loop_word] != '\0'){
before_date[cnt][loop_word] = getc_word[loop_word];
loop_word++;
}
loop_word++;
}
fclose(fp);
return before_date;
}