基本的に、私のプログラムは、ユーザーが開きたいファイルの名前を入力するように求めます。私のプログラムはそのファイルを開き、その内容を 2D 配列にスキャンすることになっています。しかし、ユーザーが指定したファイルをプログラムが開くようにするにはどうすればよいでしょうか。これまでの私のコードは次のとおりです。
#include <stdio.h>
#include <string.h>
FILE *open_file(int ar[3][4]);
int main()
{
FILE *fp;
int ar[3][4];
fp = open_file(ar);
}
FILE *open_file(int ar[3][4])
{
FILE *fp;
int i;
char file[80];
printf("Please input file name ");
scanf("%s", &file); //am I supposed to have written ("%s", file) instead?
fp = fopen("%s", "r");// very confused about this line; will this open the file?
for (i = 0; i < 12; i++)
fscanf(fp, "%d", &ar[i][]); //how do you scan the file into a 2D array?
}
malloc を使用するには、fp = (int *)malloc(sizeof(int));? のように記述する必要があります。