(2005-05の一覧)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
2005-05-25 Wed (他の年の同じ日: 2011)
ファイルIOで1行ずつの処理は
2005-05-25-1 / カテゴリ: [programming][c] / [permlink]
fgets を使って、\nまで読んだのか、\0まで読んだのか、そうでないのかをチェック
input_buf = (char *)malloc(sizeof(char) * cur_mem_size);
if (input_buf == NULL) {
perror("memory alocate error(input_buf)\n");
exit(1);
}
// EOF まで読み込み
while (!feof(fp)) {
if (fgets(read_buf, INPUT_BUFFER, fp)) {
strcat(input_buf, read_buf);
if (!feof(fp) && input_buf[strlen(input_buf) - 1] != '\n') {
// EOFでなく かつ 行の途中までしか読めなかった
cur_mem_size += INPUT_BUFFER;
printf("%d\n", cur_mem_size);
input_buf = (char *)realloc(input_buf, sizeof(char) * cur_mem_size);
if (input_buf == NULL) {
perror("realloc error\n");
exit(1);
}
}
else {
// EOFまたは行末まで読んだ
printf("[%s]", input_buf); // 行に対する処理
input_buf[0] = '\0';
}
}
}
cur_mem_size ずつファイルを読み込み、行末までよんでなかったら、バッファを realloc して更に読む、というやり方。こんなんで良いのだろうか。[
コメント ]
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
最終更新時間: 2013-05-02 16:12
