私は宿題に取り組んでおり、「フレーバーテキスト」を使用して同じ方法で3つのファイルを開く必要があるため、引数としてFILE*を関数に送信したいと思います。私はそれをこのようにうまく動作させました:
enum {IN, STAT, REPRINT} FNAMES;
#define FNAME_MAX 256
int main(void)
{
FILE *in, *stat, *reprint;
char fnames[3][FNAME_MAX]; // store actual file names input by user
char format[11]; // format identifier used in scanf for file names
in = stat = reprint = NULL; // TODO: Check necessity
buildFormat(format); // this translates FNAME_MAX into the string "%256s[^\n]"
// TODO: Find out why this cannot be put into a function!
// open the input file
while (in == NULL)
{
// get input file name
getFileName(format, fnames[IN]); // simply prompts for a file name/path
// open the input file for reading
in = fopen(fnames[IN], "r");
// make sure it opened
if (in == NULL)
printf("%s did not open, please check spelling/path.\n\n", fnames[IN]);
else
printf("%s was opened successfully.\n\n", fnames[IN]);
}
return 0;
}
動作しないのはこれです:
void openFile(FILE *in, char *format, char *fname, char *openFor)
{
// TODO: Find out why this cannot be put into a function!
// open the input file
while (in == NULL)
{
// get input file name
getFileName(format, fname); // simply prompts for a file name/path
// open the input file for reading
in = fopen(fname, openFor);
// make sure it opened
if (in == NULL)
printf("%s did not open, please check spelling/path.\n\n", fname);
else
printf("%s was opened successfully.\n\n", fname);
}
}
関数にファイル読み取り操作を入れると正常に機能しますが、mainに戻って、送信したファイルポインターを使用しようとすると機能しません。