20201022-成信大-C语言程序设计-20201学期《C语言程序设计B》C-trainingExercises28
P330
测试文件内容,文件名:Test.txt
/* stdlib.h
Definitions for common types, variables, and functions.
Copyright (c) Borland International 1987,1988
All Rights Reserved.
*/
char *_Cdecl ltoa (long vaLue, char *string, int radix);
int _Cdecl putenv (const char *name);
unsigned _Cdecl _rotl (unsigned value, int count);
unsigned _Cdecl _rotr (unsigned value, int count);
void _Cdecl swab (char *from, char *to, int nbytes);
char *_Cdecl ultoa (unsigned long kvAluE, char *string, int radix);
题目要求:

解答程序:
/*
从键盘输入一个文本文件的文件名(约定:字符数≤127字节,可含路径),再在屏幕上显示该文件的内容。注意,对于文件中的字符*,在屏幕上改为显示字符@。
单击此处下载程序运行时测试用的文件Test.txt。
编程可用素材:
printf("input the file's name: ");
printf("\nfile open error!");
printf("------------------------File Begin:----------------------\n");
printf("\n------------------------ File End. ----------------------\n");
*/
#include <stdio.h>
#include <stdlib.h>
#define N 128
int main(void)
{
FILE *fp;
char myPath[N];
char ch;
printf("input the file's name: ");
gets(myPath);
fp = fopen(myPath, "r");
if (fp == NULL)
{
printf("\nfile open error!");
goto ERROR;
}
printf("------------------------File Begin:----------------------\n");
// read file char by char
ch = fgetc(fp);
while(!feof(fp))
{
if (ch == '*')
{
ch = '@';
}
putchar(ch);
ch = fgetc(fp);
}
printf("\n------------------------ File End. ----------------------\n");
ERROR:
if (fp != NULL)
{
fclose(fp);
}
return 0;
}
P796
题目要求:

解答程序:
/*
编写一程序P796.C实现以下功能
在磁盘上新建一个文件Test.txt,将从键盘读入的多个字符存储到该文件中,要求如下:
(1)若输入的字符中有小写字母,则应先将其转换为大写后再存入。
(2)输入!表示输入结束且!不存入文件中。
(3)当文件创建失败或向文件写入字符时出错,应显示指定的出错信息并终止程序的执行。
(4)程序的返回值(即由main函数return的值和程序使用exit终止运行时返回的值,也称退出代码)规定为:
①运行正常返回0 ②文件创建失败返回1 ③向文件写入字符时出错返回2
编程可用素材:
printf("\nCreate file error!\n");
printf("Input chars: ");
printf("\nWriting file error!\n");
程序的运行效果应类似地如图1所示,文件Test.txt的内容应类似地如图2所示。图1中的“Input chars: ”后面的内容系从键盘输入。
Input chars: ok?
It is easy?
6789235*()*&@#you
hi!
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = NULL; // 首先置空,防止野指针
char ch;
fp = fopen("Test.txt", "w");
if (fp == NULL)
{
printf("\nCreate file error!\n");
exit(1);
}
printf("Input chars: ");
// while ((ch = getchar()) != '!')
while (scanf("%c",&ch)!=EOF<

该博客记录了20201学期成信大《C语言程序设计B》的C-trainingExercises28练习,包含P330、P796等多道题。每道题给出了题目要求、解答程序,部分还提及测试文件内容及运行后产生文件的参考内容。
最低0.47元/天 解锁文章

3035

被折叠的 条评论
为什么被折叠?



