C primer plus自用知識點整理(第八章)字符輸入輸出函數、輸入驗證(混合輸入)

最近在看C primer plus(加深鞏固自己的C語言技巧,爲以後學習C++打個基礎)。
裏面知識針對自己以後要查的點整理出來。
使用工具:visual studio 2013
第二、三章內容:https://blog.csdn.net/answerMack/article/details/103766020
第四章內容:https://blog.csdn.net/answerMack/article/details/103805900
第五章內容:https://blog.csdn.net/answerMack/article/details/103855794
第六章內容:https://blog.csdn.net/answerMack/article/details/103870182
第七章內容:https://blog.csdn.net/answerMack/article/details/103891048

主要介紹輸入\輸出函數(I/O函數):
輸入函數:scanf()、getchar();
輸出函數:printf()、putchar();
在這裏插入圖片描述

getchar()、putchar();

爲單字符IO,每次只處理一個字符。也是絕大多數文本(即,普通文字)處理程序所用的核心方法。與頭文件stdio.h相關聯。不是真正的函數,被定義爲供預處理器使用的宏。詳解在16章。
C程序如何處理鍵盤輸入,尤其是緩衝和標準輸入文件的概念。

緩衝區

在這裏插入圖片描述在這裏插入圖片描述


爲什麼要有緩衝?
1、把若干字符作爲一個塊傳輸比逐個發送字符節約時間
2、若打錯字符,可直接通過鍵盤修正錯誤。當按下Enter時,傳輸的是正確的輸入。


在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述
本書假設所有的輸入都是緩衝輸入

結束鍵盤輸入

在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
從文件開始學習如何階數文件:

在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述在這裏插入圖片描述

重定向和文件

在這裏插入圖片描述
在這裏插入圖片描述

Unix系統重定向

系統重定向輸入:符號爲“<”
在這裏插入圖片描述
win10系統重定向:
在這裏插入圖片描述
在win10的cmd命令行中執行:
在這裏插入圖片描述
打印出word.txt文件內容:
在這裏插入圖片描述
系統重定向輸出:符號爲“>”
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述
覆蓋掉之前的內容,替換了新的內容:
在這裏插入圖片描述
組合重定向
在這裏插入圖片描述
重定向運算符原則:
在這裏插入圖片描述
符號“>>”和" | "
在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述

用程序直接打開文件

fopen等函數。具體介紹見後面幾章。


fopen和fopen_s用法的比較
fopen 和 fopen_s
fopen用法: fp = fopen(filename,“w”)。
fopen_s用法:,須定義另外一個變量errno_t err,然後err = fopen_s(&fp,filename,“w”)。
返回值: fopen打開文件成功,返回文件指針(賦值給fp),打開失敗則返回NULL值;
fopen_s打開文件成功返回0,失敗返回非0。
在定義FILE * fp 之後,fopen的用法是: fp = fopen(filename,“w”)。而對於fopen_s來說,還得定義另外一個變量errno_t err,然後err = fopen_s(&fp,filename,“w”)。返回值的話,對於fopen來說,打開文件成功的話返回文件指針(賦值給fp),打開失敗則返回NULL值;對於fopen_s來說,打開文件成功返回0,失敗返回非0。


在這裏插入圖片描述

緩衝輸入

在這裏插入圖片描述
在這裏插入圖片描述

混合數值和字符輸入

在這裏插入圖片描述
在這裏插入圖片描述
程序在這裏插入圖片描述
結果:
在這裏插入圖片描述

輸入驗證

在這裏插入圖片描述
 在這裏插入圖片描述
在這裏插入圖片描述

// checking.c -- validating input
#include <stdio.h>
#include <stdbool.h>
// validate that input is an integer
long get_long(void);
// validate that range limits are valid
bool bad_limits(long begin, long end,
	long low, long high);
// calculate the sum of the squares of the integers
// a through b
double sum_squares(long a, long b);
int main(void)
{
	const long MIN = -10000000L;  // lower limit to range
	const long MAX = +10000000L;  // upper limit to range
	long start;                   // start of range
	long stop;                    // end of range
	double answer;

	printf("This program computes the sum of the squares of "
		"integers in a range.\nThe lower bound should not "
		"be less than -10000000 and\nthe upper bound "
		"should not be more than +10000000.\nEnter the "
		"limits (enter 0 for both limits to quit):\n"
	);
	printf("lower limit: ");
	start = get_long();
	printf("upper limit: ");
	stop = get_long();
	while (start != 0 || stop != 0)
	{
		if (bad_limits(start, stop, MIN, MAX))
			printf("Please try again.\n");
		else
		{
			answer = sum_squares(start, stop);
			printf("The sum of the squares of the integers ");
			printf("from %ld to %ld is %g\n",
				start, stop, answer);
		}
		printf("Enter the limits (enter 0 for both "
			"limits to quit):\n");
		printf("lower limit: ");
		start = get_long();
		printf("upper limit: ");
		stop = get_long();
	}
	printf("Done.\n");
	getchar();
	return 0;
}

long get_long(void)
{
	long input;
	char ch;

	while (scanf_s("%ld", &input) != 1)
	{
		while ((ch = getchar()) != '\n')
			putchar(ch);  // dispose of bad input
		printf(" is not an integer.\nPlease enter an ");
		printf("integer value, such as 25, -178, or 3: ");
	}

	return input;
}

double sum_squares(long a, long b)
{
	double total = 0;
	long i;

	for (i = a; i <= b; i++)
		total += (double)i * (double)i;

	return total;
}

bool bad_limits(long begin, long end,
	long low, long high)
{
	bool not_good = false;

	if (begin > end)
	{
		printf("%ld isn't smaller than %ld.\n", begin, end);
		not_good = true;
	}
	if (begin < low || end < low)
	{
		printf("Values must be %ld or greater.\n", low);
		not_good = true;
	}
	if (begin > high || end > high)
	{
		printf("Values must be %ld or less.\n", high);
		not_good = true;
	}

	return not_good;
}

菜單瀏覽

在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

/* menuette.c -- menu techniques */
#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
	int choice;
	void count(void);

	while ((choice = get_choice()) != 'q')
	{
		switch (choice)
		{
		case 'a':  printf("Buy low, sell high.\n");
			break;
		case 'b':  putchar('\a');  /* ANSI */
			break;
		case 'c':  count();
			break;
		default:  printf("Program error!\n");
			break;
		}
	}
	printf("Bye.\n");

	return 0;
}

void count(void)
{
	int n, i;

	printf("Count how far? Enter an integer:\n");
	n = get_int();
	for (i = 1; i <= n; i++)
		printf("%d\n", i);
	while (getchar() != '\n')
		continue;
}

char get_choice(void)
{
	int ch;

	printf("Enter the letter of your choice:\n");
	printf("a. advice           b. bell\n");
	printf("c. count            q. quit\n");
	ch = get_first();
	while ((ch < 'a' || ch > 'c') && ch != 'q')
	{
		printf("Please respond with a, b, c, or q.\n");
		ch = get_first();
	}

	return ch;
}

char get_first(void)
{
	int ch;

	ch = getchar();
	while (getchar() != '\n')
		continue;

	return ch;
}

int get_int(void)
{
	int input;
	char ch;

	while (scanf_s("%d", &input) != 1)
	{
		while ((ch = getchar()) != '\n')
			putchar(ch);  // dispose of bad input
		printf(" is not an integer.\nPlease enter an ");
		printf("integer value, such as 25, -178, or 3: ");
	}

	return input;
}

在這裏插入圖片描述

本章小結

在這裏插入圖片描述

發佈了43 篇原創文章 · 獲贊 23 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章