基本上,我的程序会提示用户输入他想要打开的文件的名称。我的程序应该打开该文件并将其内容扫描到二维数组中。但是你怎么做才能让程序打开用户指定的文件呢?到目前为止,这是我的代码:
#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));?