C言語なら、俺に聞け! <13>
■ このスレッドは過去ログ倉庫に格納されています
0239238のつづき
NGNGif (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
/* strtok は引数で渡した文字列破壊するので、コピーしる */
strcpy(line_tmp,line);
/* 単語数カウント */
count=0;
for(word = strtok(line_tmp," \t"); word; word = strtok(NULL," \t")) {
count++;
}
/* 単語数分のポインタ領域確保 */
line_element_ptr = hoge[lines] = (char **)malloc( (count+1) * sizeof(ch\
ar*) );
if (!line_element_ptr) {
puts("メモリねーよ");
exit(1);
}
/* 単語を複製し、そのアドレスをさっき確保した領域に入れてく */
for(word = strtok(line," \t"); word; word = strtok(NULL," \t")) {
*line_element_ptr++ = strdup(word);
}
*line_element_ptr = NULL;
lines++;
}
/* 行バッファ開放 */
free(line);
free(line_tmp);
/* ついでに表示するよぅ(ついでにメモリ開放しる) */
for(i=0; i<lines; i++) {
for(j=0;hoge[i][j];j++) {
printf("[%s]",hoge[i][j]);
if (hoge[i][j+1]) printf(",");
free(hoge[i][j]);
}
free(hoge[i]);
puts("");
}
}
■ このスレッドは過去ログ倉庫に格納されています