ファイルから入力を読み取り、malloc と realloc を使用して各文字列を配列に入れようとしています。したがって、入力ファイルが次の場合:
alex
john
jane
smith
配列の内容には {"alex\n", "john\n", "jane\n", "smith\n"} が含まれます。これまでのところ、私は次のようなことをしました:
int n=0;
int size=1;
File *fp = fopen(args[0],"r");
int c;
char* inputFile;
inputFile = (char*) malloc(size);
if(fp==0){
fprintf(stderr, "Cannot open file!\n");
return -1;}
else{
do{
c = fgetc(fp);
inputFile = (char*) realloc(inputFile, size+1);
inputFile[n]=c;
n++;
size++;
}while(c!=EOF);
このアルゴリズムは、{'a','l','e','x','\n','j','o','h','n',' のような配列になると思います\n','j','a','n','e','\n','s','m','i','t','h','\n'}
inputFile を 2 次元配列にするにはどうすればよいですか? reallocで何をすべきですか?