C primer plus (第六版)第十一章 编程练习第16题
题目:
16.编写⼀个程序读取输⼊,直⾄读到⽂件结尾,然后把字符串打印出来。该程序识别和实现下⾯的命令⾏参数:
-p 按原样打印
-u 把输⼊全部转换成⼤写
-l 把输⼊全部转换成⼩写
如果没有命令⾏参数,则让程序像是使⽤了-p参数那样运⾏。
思路:
1. 参照之前命令行参数形式程序,题目14, 题目13等的命令行参数形式程序;
2. 借助strncmp()函数,将命令行输入的命令-p, -u, -l进行比较,然后用switch case执行相应的操作;
3. 文件打开与屏幕输出参考原书程序清单8.3 file_eof.c程序:
// file_eof.c --打开⼀个⽂件并显⽰该⽂件#include <stdio.h>#include <stdlib.h> // 为了使⽤exit()int main(){int ch;FILE * fp;char fname[50]; // 存储⽂件名printf("Enter the name of the file: ");scanf("%s", fname);fp = fopen(fname, "r"); // 打开待读取⽂件if (fp == NULL) // 如果失败{printf("Failed to open file. Bye\n");exit(1); // 退出程序}// getc(fp)从打开的⽂件中获取⼀个字符while ((ch = getc(fp)) != EOF)putchar(ch);fclose(fp); // 关闭⽂件return 0;}4. 在命令行模式下进行测试。
程序:
/*--------------------------------------------------------------------------------
题目:
16.编写⼀个程序读取输⼊,直⾄读到⽂件结尾,然后把字符串打印出来。该程序
识别和实现下⾯的命令⾏参数:
-p 按原样打印
-u 把输⼊全部转换成⼤写
-l 把输⼊全部转换成⼩写
如果没有命令⾏参数,则让程序像是使⽤了-p参数那样运⾏。
--------------------------------------------------------------------------------
思路:
1. 参照之前命令行参数形式程序,题目14, 题目13等的命令行参数形式程序;
2. 借助strncmp()函数,将命令行输入的命令-p, -u, -l进行比较,然后用switch case执行相应的操作;
3. 文件打开与屏幕输出参考原书程序清单8.3 file_eof.c程序:// file_eof.c --打开⼀个⽂件并显⽰该⽂件
#include <stdio.h>
#include <stdlib.h> // 为了使⽤exit()
int main()
{int ch;FILE * fp;char fname[50]; // 存储⽂件名printf("Enter the name of the file: ");scanf("%s", fname);fp = fopen(fname, "r"); // 打开待读取⽂件if (fp == NULL) // 如果失败{printf("Failed to open file. Bye\n");exit(1); // 退出程序}// getc(fp)从打开的⽂件中获取⼀个字符while ((ch = getc(fp)) != EOF)putchar(ch);fclose(fp); // 关闭⽂件return 0;
}
4. 在命令行模式下进行测试。
--------------------------------------------------------------------------------*/#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>int main(int argc, char *argv[])
{int choose; //程序执行选择FILE * fp;char fname[50]; //文件名数组char ch;if(argc != 2){ //检查是否输入了附加命令printf("Wrong command.\n");return 0;}if ((argc == 2) && !strncmp("-p", argv[1],2)) //strncmp()符合返回0,所以用!strncmp()choose = 1;else if ((argc == 2) && !strncmp("-u", argv[1],2))choose = 2;else if ((argc == 2) && !strncmp("-l", argv[1],2))choose = 3; elseputs("Wrong command."); //其他附加命令形式提示错误switch (choose){case 1: //按原样打印printf("Enter the name of the file: ");scanf("%s", fname);fp = fopen(fname, "r"); // 打开待读取⽂件if (fp == NULL) // 如果失败{printf("Failed to open file. Bye\n");exit(1); // 退出程序}// getc(fp)从打开的⽂件中获取⼀个字符while ((ch = getc(fp)) != EOF)putchar(ch);fclose(fp);break;case 2: //全部转大写printf("Enter the name of the file: ");scanf("%s", fname);fp = fopen(fname, "r"); // 打开待读取⽂件if (fp == NULL) // 如果失败{printf("Failed to open file. Bye\n");exit(1); // 退出程序}// getc(fp)从打开的⽂件中获取⼀个字符while ((ch = getc(fp)) != EOF){ch = toupper(ch);putchar(ch);}fclose(fp);break;case 3: //全部转小写printf("Enter the name of the file: ");scanf("%s", fname);fp = fopen(fname, "r"); // 打开待读取⽂件if (fp == NULL) // 如果失败{printf("Failed to open file. Bye\n");exit(1); // 退出程序}// getc(fp)从打开的⽂件中获取⼀个字符while ((ch = getc(fp)) != EOF){ch = tolower(ch);putchar(ch);}fclose(fp);break;default:break;}return 0;
}运行:
1. 因为是采用命令行执行方式,新建一个words.txt文件,输入如下测试文字:
She walks in beauty, like the night
Of cloudless climes and starry skies...
Lord Byron
2. 将编译的程序改为word.exe, 在shell终端命令模式下测试程序;
3. 先输入word -u或-l或-p,进入程序提示输入文件名称,输入words.txt(放在和word.exe同一个目录下面),终端输出结果:

