|
|
检测点12-2
1.编写程序,以文本模式将文件myfile1.txt的内容读出并写到标准输出。
参考答案:
# include <stdio.h>
int main (void)
{
FILE * hfile = fopen ("myfile1.txt", "r");
if (hfile == NULL) return -1;
char c;
while ((c = fgetc (hfile)) != EOF)
fputc (c, stdout);
fclose (hfile);
}
2.编写程序,以二进制模式将文件myfile1.txt的内容读出并写到标准输出。
参考答案:
# include <stdio.h>
int main (void)
{
FILE * hfile = fopen ("myfile1.txt", "rb");
if (hfile == NULL) return -1;
char c;
while ((c = fgetc (hfile)) != EOF)
fputc (c, stdout);
fclose (hfile);
}
3.编写程序,以文本模式将文件myfile1.txt的内容读出并写到标准输出,但是要将回车符输出为“<CR>”,将换行符输出为“<LF>”。
参考答案:
# include <stdio.h>
int main (void)
{
FILE * hfile = fopen ("myfile1.txt", "r");
if (hfile == NULL) return -1;
char c;
while ((c = fgetc (hfile)) != EOF)
if (c == '\r') printf ("<CR>");
else if (c == '\n') printf ("<LF>");
else fputc (c, stdout);
fclose (hfile);
}
4.编写程序,以二进制模式将文件myfile1.txt的内容读出并写到标准输出,但是要将回车符输出为“<CR>”,将换行符输出为“<LF>”。
参考答案:
# include <stdio.h>
int main (void)
{
FILE * hfile = fopen ("myfile1.txt", "rb");
if (hfile == NULL) return -1;
char c;
while ((c = fgetc (hfile)) != EOF)
if (c == '\r') printf ("<CR>");
else if (c == '\n') printf ("<LF>");
else fputc (c, stdout);
fclose (hfile);
}
|
|