阅读以下程序,对程序功能的描述中正确的是
#icnlude
int main()
{
FILE *in, *out;
char ch, infile[10], outfile[10];
printf("Enter the infile name:");
scanf("%s", infile);
printf("Enter the outfile name:");
scanf("%s", outfile);
if ((in = fopen(infile, "r")) == NULL)
{
printf("cannot open infile.");
exit(0);
}
if ((out = fopen(outfile, "w")) == NULL)
{
printf("cannot open outfile.");
exit(0);
}
while (!feof(in))
{
fputc(fgetc(in), out);
}
fclose(in);
fclose(out);
return 0;
}
#icnlude
int main()
{
FILE *in, *out;
char ch, infile[10], outfile[10];
printf("Enter the infile name:");
scanf("%s", infile);
printf("Enter the outfile name:");
scanf("%s", outfile);
if ((in = fopen(infile, "r")) == NULL)
{
printf("cannot open infile.");
exit(0);
}
if ((out = fopen(outfile, "w")) == NULL)
{
printf("cannot open outfile.");
exit(0);
}
while (!feof(in))
{
fputc(fgetc(in), out);
}
fclose(in);
fclose(out);
return 0;
}
举一反三
- 要求打开文件file.dat,并能够写入数据,正确的语句是:() A: ifstream infile(“file.dat”,ios::in); B: ifstream infile(“file.dat”,ios::out); C: ofstream outfile(“file.dat”,ios::in); D: ofstream outfile(“file.dat”,ios::out);
- 要求打开文件f:\myfile.dat,并能够写入数据,正确的语句是( )。 A: ifstream infile("f:\\myfile.dat", ios::in); B: ifstream infile("f:\\myfile.dat ", ios::out); C: ofstream outfile("f:\\myfile.dat ", ios::in); D: fstream infile("f:\\myfile.dat ", ios::in | ios::out);
- 以下程序希望把从终端输入的字符输出到名为abc.txt的文件中,直到从终端读入字符#号时结束输入和输出操作,但程序有错。出错的原因是#include int main(){ FILE *fout; char ch; fout=fopen(abc.txt,w); if (fout == NULL) { printf(cannot open infile. ); exit(0); } ch=fgetc(stdin); while(ch!=#) { fputc(ch,fout); ch=fgetc(stdin); } fclose(fout); return 0;}
- 中国大学MOOC: 以下程序希望把从终端输入的字符输出到名为abc.txt的文件中,直到从终端读入字符#号时结束输入和输出操作,但程序有错。出错的原因是#include int main(){ FILE *fout; char ch; fout=fopen(abc.txt,w); if (fout == NULL) { printf(cannot open infile. ); exit(0); } ch=fgetc(stdin); while(ch!=#) { fputc(ch,fout); ch=fgetc(stdin); } fclose(fout); return 0;}
- 要求打开文件f:\myfile。dat,并能够写入数据,正确的语句是( )。 A: ofstream outfile("f:\\myfile.dat ", ios::in); B: ifstream infile("f:\\myfile.dat", ios::in); C: fstream infile("f:\\myfile.dat ", ios::in | ios::out); D: ifstream infile("f:\\myfile.dat ", ios::out);