C语言第10章文件

合集下载

C语言程序设计第四版PPT-谭浩强

C语言程序设计第四版PPT-谭浩强

文件f2的 文件信息区
文件f3的 文件信息区
文件的基本知识——文件的操作 写文件:打开-写-关闭 读文件:打开-读-关闭
文件的打开
功能:为文件建立相应的信息区(存放 文件信息)和文件缓冲区(暂时存放输入 输出的数据) 函数: fopen(文件名,使用文件方式); 例: fopen("a1.dat","r");
第10章 文件
本章内容
10.1 文件的基本知识 10.2 打开与关闭文件 10.3 顺序读写数据文件 10.4 随机读写数据文件 10.5 文件读写的出错检测
文件的基本知识——输入输出
之前各章中处理的数据 ➢输入:键盘->内存 ➢输出:内存->显示器 实际上也可能是 ➢输入:外部设备->内存(读文件) ➢输出:内存->外部设备(写文件) 操作系统把各种设备都统一作为文件处理
文件
顺序文件的读写——读写字符串
例10.3 从键盘输入3个字符串,排序,把排
序后的字符串写入文件(P.342-343) 读上面写好的文件(P.344)
顺序文件的读写——格式化读写
fprintf(文件指针,格式字符串,输出表列); fscanf (文件指针,格式字符串,输入表列);
例: fprintf (fp,"%d,%6.2f",i,f); fscanf (fp,"%d,%f",&i,&f);
用文件扩展名”
文件的基本知识——流式文件
C语言把文件看作是字符(或字 节)的序列。
文件以“流”的形式在程序与操 作系统间流动。
文件的基本知识——数据的形式
根据数据的组织形式,数据文件可分为 ASCII文件:数据逐个字符的ASCII码 输出到外存,也称文本文件 二进制文件:内存中二进制形式的数据 直接输出到外存

《C语言程序设计教程》PPT课件

《C语言程序设计教程》PPT课件
② 标准输出文件,文件指针为stdout,系统指定为 显示器。
③ 标准错误输出文件,文件指针为stderr,系统指 定为显示器,输出错误信息。
10.1 文件的概述
11
10.1.2 文件的打开与关闭
操作文件的一般步骤
打开文件 读写文件 关闭文件 打开量文的件函C:语数建配言,立一的用用个输于户文入完程件输成序缓出对与冲函数文区数据件。库文的中件联提的系供建,了立为大、文件分 读写数文据件的:读是写指、对数文据件的的追读加、等写操、作追。加和定位操作。 关闭文件在:程切序断中文调件用与这程些序函的数联时系,,必释须放先文用件缓冲
}
FFIILLE;E *文件指针变量/名* ;This is the FILE object */
10.1 文件的概述
10
10.1.1 文件和文件指针
4. 标准文件
标准文件是外部设备中的三个特殊的设备文件。它们 的文件指针是系统定义的,并由系统自动打开和关闭。这 三个标准文件分别是:
① 标准输入文件,文件指针为stdin,系统指定为 键盘。
include命区令。包含stdio.h文件。
10.1 文件的概述
12
10.1.2 文件的打开与关闭
1. 文件打开函数fopen( )
常用的调用形式: FILE *fp; fp = fopen(文件名, 文件使用方式); 文件名:需要打开的文件名称(字符串)。 文件使用方式:是具有特定含义的符号。
缓冲区cha地r 址和大小f等d; 。 /* File descriptor
*/
FILE在类usnhCso型语irgt是n言ed系中ch统a,r定无hb义os论li的dz;e对;,磁包//**盘含UBnu文g在fefte件scrtcs还dhiazi是reo.i设fh头n备o 文b文uf件*f件/er中*/

C语言完整资料

C语言完整资料
2.2.2 常量
2.2.2.1 字面常量(直接常量)
字面常量是直接表示的常量。
2.2.2.2 符号常量
符号常量是用一个标识符代表一个常量,使用编译预处理命令#define定 义,定义格式为:
#define 常量名 字面常量
精选版ppt
2-311
2.2.3 变量
在程序运行过程中,其值可以改变的量称为变量。 C语言规定,变量要先定义后使用。变量的定义一般在函数的声明部分,格式为:
2. 函数体: 函数体为函数首部下面的花括号{……}内的部分。 函数体一般由声明部分和执行部分构成:声明部分定义所用到的变量,声明所调用的函数等; 执行部分有若干语句组成。
1.2.2.3 C语言程序的执行
C语言程序总是从main()函数的第一条执行语句开始执行,执行完main()函数的最后一 条语句,则整个程序执行结束,而其他函精数选则版通pp过t 嵌套调用得以执行。
1-34
1.2.3 C语言程序书写格式 1. 行的书写格式自由,可从一行的任何位置开始书写。 2. 一行可书写多条语句,一条语句也可分写在几行上。 3. 所有语句都必须以分号“;”结束。 4. 允许使用注释。 C语言的注释格式有两种://,/* …… */。 5. 在书写程序时,通常采用缩格形式书写分层结构的程序。
本章主要介绍C语言的特点,C语言程序的结构与书写规则,C程序上机步骤,编译预 处理,算法的概念及表示。
精选返版回pp目t 录
1-12
1.1 语言的特点
1.语言简洁、紧凑,使用方便、灵活 2.运算符丰富 3.数据类型丰富 4.程序设计结构化、模块化 5.语法限制不太严格,程序设计自由度大 6.C语言允许直接访问物理地址,能进行位(bit)操作 7.生成的目标代码质量高,程序执行效率高 8.可移植性好

计算机二级C语言辅导 10章 字符串

计算机二级C语言辅导 10章 字符串

例2. 删除一个字符串中的一个字符。 字符串由标准输入得到,欲删除的字符也由标准输入得到
main ( ) { char s[20],c; int i,j; gets(s); c=getchar( ); for (i=j=0;s[i]!=„\0‟;i++) if (s[i]!=c) s s[j++]=s[i]; s[j]=„\0‟; c=l }
p
i j
i j
i j
i
j
i
W
0
o
1
r
2
l
3
d
4
\0
5
例2. 删除一个字符串中的一个字符。 字符串由标准输入得到,欲删除的字符也由标准输入得到
main ( ) { char s[20],c; int i,j; gets(s); c=getchar( ); for (i=j=0;s[i]!=„\0‟;i++) if (s[i]!=c) s s[j++]=s[i]; s[j]=„\0‟; c=l }
H o w \0
0 1 2 3 4 5 6 7 8 9 10 11 12
字符数组
五、字符串处理函数: 1.puts(字符数组); 将字符串输出到终端,在输出时将字符串结束标记‘\0‟转换成‘\ char str[ ]={“China\nBeijing”}; puts(str); 输出:China Beijing 2.gets(字符数组)--读入全部字符(包括空格),直到遇到回车符为止 从终端输入一个字符串到字符数 如果输入以下12个字符: 组,并且得到一个函数值,该函 数值是字符数组的起始地址。
第十章 字符串
在C语言中,没有字符串这种数据类型, 因此可以将字符串存放在字符数组中。 这是处理字符串的方法之一。

谭浩强版C语言的第十章《指针》答案

谭浩强版C语言的第十章《指针》答案

谭浩强版C语言的第十章《指针》答案第十章《指针》答案如下inc/testPtr.h#include <string.h>#include <ctype.h>#include <math.h>#include <assert.h>#define SIZE 1024int a2i(char *start, char *end){int size = 0, ret = 0;long base = 0;size = end - start + 1;base = (long)pow(10, size - 1);while(size-- > 0){ret += (*start++ - '0') * base;base /= 10;}return ret;}int extraNum(char *str, int arr[]){int ite = 0, counter = 0;char *start = NULL, *end = NULL;while(*str != '\0'){if(isdigit(*str)){start = end = str;while( isdigit(*end) && *end != '\0'){++end;}arr[ite++] = a2i(start, end-1);str = end;}else{str++;}}return ite;}int sortStr(char *arr[], int size){int ite1 = 0, ite2 = 0, minPos = 0;char *tmp;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ++ite2 ){if( strcmp(arr[ite2], arr[minPos]) < 0 ){minPos = ite2;}}if(minPos != ite1){tmp = arr[minPos];arr[minPos] = arr[ite1];arr[ite1] = tmp;}}return 0;}int sort(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( arr[ite2] < arr[minPos] ){minPos = ite2;}}if(minPos != ite1){tmp = arr[ite1];arr[ite1] = arr[minPos];arr[minPos] = tmp;}}return 0;}int sortPtr(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( *(arr + ite2) < *(arr + minPos) ){minPos = ite2;}}if(minPos != ite1){tmp = *(arr + ite1);*(arr + ite1) = *(arr + minPos);*(arr + minPos) = tmp;}}return 0;}int getMultiArr(int arr[][5], int n){int i = 0, j = 0, min = 0, tmp = 0, size = 5 * n;int *p = NULL, pos[size];/* copy */p = (int *)malloc(size * sizeof(int));assert(p != NULL);memcpy(p, arr, size * sizeof(int));/* sort */for(i = 0; i < size - 1; i++){min = i;for(j = i + 1; j < size; j++){if( *(p + j) < *(p + min)){min = j;}}if(min != i){tmp = *(p + min);*(p + min) = *(p + i);*(p + i) = tmp;}}/* move */for(i = 0; i < n; i++){for(j = 0; j < 5; j++){if( *p == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][0];arr[0][0] = tmp;continue;}if( *(p + 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][4];arr[0][4] = tmp;continue;}if( *(p + 2) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][0];arr[n - 1][0] = tmp;continue;}if( *(p + 3) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][4];arr[n - 1][4] = tmp;continue;}if( *(p + size - 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n/2][2];arr[n/2][2] = tmp;continue;}}}free(p);p = NULL;return 0;}int statStr(char *str){int upper = 0, lower = 0, space = 0, num = 0, other = 0;while(*str != '\0'){if(isdigit(*str)){num++;}else if (isupper(*str)){upper++;}else if (islower(*str)){lower++;}else if (isspace(*str)){space++;}else{other++;}str++;}assert(3 == upper);assert(5 == lower);assert(10 == num);assert(2 == space);assert(6 == other);return 0;}int average(int(*stu)[6], int classNum, int stuNum){int i = 0, ave = 0;for(i = 0; i < stuNum; i++){ave += (*(stu + i))[classNum];}ave /= stuNum;return ave;}int searchStu(int(*stu)[6], int stuNum){int counter = 0, i = 0, j = 1, stuCounter = 0;for (i = 0; i < stuNum; i++){counter =0;for(j = 1; j < 6; j++){if( (*(stu + i))[j] < 60 ){counter++;}}if(counter >= 2){stuCounter++;}}return stuCounter;}int moveInt(int arr[], int n, int m){int i = 0, *p = NULL;p = (int*)malloc(n * sizeof(int));assert(p != NULL);memcpy(p + m, arr, (n - m) * sizeof(int));memcpy(p, arr + n -m , m * sizeof(int));memcpy(arr, p, n * sizeof(int));free(p);p = NULL;return 0;}int myStrcmp(char *p1, char*p2){int ret = 0;while((*p1 != '\0') && (*p2 != '\0') && ( *p1 == *p2 ) ) {p1++;p2++;}if(*p1 == '\0'){ret = -1;}else if (*p2 == '\0'){ret = 1;}else{ret = (*p1 - *p2) > 0 ? 1 : -1;}return ret;}int revArr(int a[], int size){int tmp = 0, *start = NULL, *end = NULL;start = a;end = start + size - 1;size = size / 2;while(size >= 0){tmp = *(start + size);*(start + size) = *(end - size);*(end - size) = tmp;size--;}return 0;}char *getMonth(char *month[], int which) {assert(which <= 12);return ( *(month + which - 1));}int getStr(char *dest, char* src, int m) {int len = 0;len = strlen(src) + 1 - m;src = src + m - 1;memcpy(dest, src, len * sizeof(char));return 0;}int removePer3(int arr[], int size){int i = 0;for(i = 0; i < size; i++){if(((arr[i]) % 3) == 0){arr[i] = 0;}}return 0;}int getMinMax(int a[], int size){int i = 0, min = 0, max = 0, tmp = 0;min = max = 0;for(i = 0; i < size; i++){if (a[i] <= a[min]){min = i;}if(a[i] >= a[max]){max = i;}}tmp = a[0];a[0] = a[min];a[min] = tmp;tmp = a[size - 1];a[size - 1] = a[max];a[max] = tmp;}int test_10_1(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sortPtr(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_1 Passed!");return 0;}int test_10_2(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable"};iRet = sortStr(arr, 3);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_2 Passed!");return 0;}int test_10_3(){int ret = 0, a[10] = {7,2,3,9,1,0,7,6,5,0};ret = getMinMax(a, 10);assert(a[0] == 0);assert(a[9] == 9);printf("\r\nTest_10_3 Passed!");return 0;}int test_10_4(){int ret = 0, arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};ret = moveInt(arr, 9, 3);assert(6 == arr[0]);assert(7 == arr[1]);assert(8 == arr[2]);assert(0 == arr[3]);assert(1 == arr[4]);assert(2 == arr[5]);assert(3 == arr[6]);assert(4 == arr[7]);assert(5 == arr[8]);printf("\r\nTest_10_4 Passed!");return 0;}int test_10_5(){int ret = 0, i = 0, a[12] = {1,2,3,4,5,6,7,8,9,10,11,12};ret = removePer3(a, 12);assert(a[2] == 0);assert(a[5] == 0);assert(a[8] == 0);assert(a[11] == 0);printf("\r\nTest_10_5 Passed!");return 0;}int test_10_7(){int ret = 0;char s2[100] = {0}, *s1 = "hello world!";getStr(s2, s1, 7);assert( strcmp(s2, "world!") == 0);printf("\r\nTest_10_7 Passed!");return 0;}int test_10_8(){int ret = 0;char *str = "a123xABC ??#$%^ 302tab5876";ret = statStr(str);if(ret == 0){printf("\r\nTest_10_8 Passed!");}return 0;}int test_10_10(){int i = 0, j = 0, ret = 0;int arr[5][5] = { \{16, 17, 18, 19, 20}, \{11, 12, 13, 14, 15}, \{1, 2, 3, 4, 5}, \{21, 22, 23, 24, 25}, \{6, 7, 8, 9, 10}, \};ret = getMultiArr(arr, 5);assert(1 == arr[0][0] );assert(2 == arr[0][4] );assert(3 == arr[4][0] );assert(4 == arr[4][4] );assert(25 == arr[2][2] );printf("\r\nTest_10_10 Passed!");return 0;}int test_10_11(){int ite = 0, iRet = 0;char *arr[10] = { "In the", \"An aud", \"The ef", \"Proces", \"Sets o", \"Unifie", \"Out-of", \"Practi", \"Ration", \"Explor" \};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An aud") == 0 );assert( strcmp(arr[1], "Explor") == 0);assert( strcmp(arr[2], "In the") == 0);assert( strcmp(arr[3], "Out-of") == 0);assert( strcmp(arr[4], "Practi") == 0);assert( strcmp(arr[5], "Proces") == 0);assert( strcmp(arr[6], "Ration") == 0);assert( strcmp(arr[7], "Sets o") == 0);assert( strcmp(arr[8], "The ef") == 0);assert( strcmp(arr[9], "Unifie") == 0);printf("\r\nTest_10_11 Passed!");return 0;}int test_10_12(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features", \"Unified Change Management (UCM),", \"Out-of-the-box process that supports best", \"Practices for change management as described in the IBM", \"Rational Unified Process. Project managers can configure", \"Explorer. For more information about Rational ClearCase"};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "Explorer. For more information about Rational ClearCase") == 0);assert( strcmp(arr[2], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[3], "Out-of-the-box process that supports best") == 0);assert( strcmp(arr[4], "Practices for change management as described in the IBM") == 0);assert( strcmp(arr[5], "Process by using one of the following") == 0);assert( strcmp(arr[6], "Rational Unified Process. Project managers can configure") == 0);assert( strcmp(arr[7], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[8], "The efforts of your team can be coordinated into a definable") == 0);assert( strcmp(arr[9], "Unified Change Management (UCM),") == 0);printf("\r\nTest_10_12 Passed!");return 0;}int test_10_14(){int ret = 0, a[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};ret = revArr(a, 11);assert(10 == a[0]);assert(9 == a[1]);assert(8 == a[2]);assert(7 == a[3]);assert(6 == a[4]);assert(5 == a[5]);assert(4 == a[6]);assert(3 == a[7]);assert(2 == a[8]);assert(1 == a[9]);assert(0 == a[10]);printf("\r\nTest_10_14 Passed!");return 0;}int test_10_15(){int ave = 0, ret = 0;int student[4][6] = { \{1, 100, 90, 80, 70, 97}, \{2, 34, 45, 56, 78, 97}, \{3, 76, 34, 68, 84, 12}, \{4, 90, 90, 90, 75, 28} \};ave = average(student, 1, 4);ret = searchStu(student, 4);assert(75 == ave);assert(2 == ret);printf("\r\nTest_10_15 Passed!");return 0;}int test_10_16(){int iRet = 0, ite = 0, arr[SIZE] = {0};char *str = "a123x456 17960? 302tab5876";iRet = extraNum(str, arr);assert(123 == arr[0]);assert(456 == arr[1]);assert(17960 == arr[2]);assert(302 == arr[3]);assert(5876 == arr[4]);printf("\r\nTest_10_16 Passed!");return 0;}int test_10_17(){int ret = 0;char *s1 = "abcd", *s2 = "abCd";ret = myStrcmp(s1, s2);assert(ret == 1);char *s3 = "aBcd", *s4 = "abCd";ret = myStrcmp(s3, s4);assert(ret == -1);char *s5 = "abcde", *s6 = "abcd";ret = myStrcmp(s5, s6);assert(ret == 1);char *s7 = "abcd", *s8 = "abcde";ret = myStrcmp(s7, s8);assert(ret == -1);char *s9 = "abcd", *s10 = "abCde";ret = myStrcmp(s9, s10);assert(ret == 1);printf("\r\nTest_10_17 Passed!");return 0;}int test_10_18(){int which = 0;char *month[12] = { \"January", \"February", \"March", \"April", \"May", \"June", \"July", \"August", \"September", \"October", \"November", \"December" \};which = 11;assert( strcmp("November", getMonth(month, which)) == 0 );which = 7;assert( strcmp("July", getMonth(month, which)) == 0 );printf("\r\nTest_10_18 Passed!");return 0;}int test_10_20(){int ite = 0, iRet = 0;char *arr[5] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features" \};iRet = sortStr(arr, 5);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "Process by using one of the following") == 0);assert( strcmp(arr[3], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[4], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_20 Passed!");return 0;}int test_10_21(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sort(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_21 Passed!");return 0;}int testPtr(){int iRet = 0;#if 0#endifiRet += test_10_1();iRet += test_10_2();iRet += test_10_3();iRet += test_10_4();iRet += test_10_5();iRet += test_10_7();iRet += test_10_8();iRet += test_10_10();iRet += test_10_11();iRet += test_10_12();iRet += test_10_14();iRet += test_10_15();iRet += test_10_16();iRet += test_10_17();iRet += test_10_18();iRet += test_10_20();iRet += test_10_21();return iRet;}src/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "../inc/testFile.h" #include "../inc/testBits.h" #include "../inc/testPtr.h"int main(){int iRet = 0;#if 0iRet += testFile();assert(iRet == 0);iRet += testBits();assert(iRet == 0);#endifiRet += testPtr();assert(iRet == 0);return 0;}。

chap10(文件)

chap10(文件)

10.2 文件操作 在对文件读、写之前必须先打开该文件, 在对文件读 、 写之前必须先打开该文件 , 使用结束后 应关闭该文件。 应关闭该文件。 一、 文件打开函数 fopen 函数调用格式: 函数调用格式: FILE *fp; fp=fopen(“文件名”,“使用方式”); 文件名” 使用方式 使用方式” 文件名 打开指定的文件,以指定的使用方式进行访问。 打开指定的文件,以指定的使用方式进行访问。 说明: 说明: 指向打开的文件后, 就可以用它来访问该文件。 用 fp 指向打开的文件后 就可以用它来访问该文件。 文件名”为任何合法的DOS文件名。 文件名。 “文件名”为任何合法的 文件名 有的C 版本只用r、 、 ,或只用rw、 而不用r+、 有的 版本只用 、w、a,或只用 、ra 而不用 、 w+、a+ 、 在程序运行时, 系统自动打开三个标准文件stdin、 在程序运行时 , 系统自动打开三个标准文件 、 stdout、stderr。 、 。
第 10 章
10.1 文件操作库函数
文件
一、 概述 C语言将文件看作是一个字符 字节 序列组成的 。 语言将文件看作是一个字符(字节 序列组成的。 语言将文件看作是一个字符 字节)序列组成的 分为ASCII文件和二进制文件两种。 文件和 两种。 分为 文件 二进制文件两种 C的“缓冲 输出 文件系统” 文件系统 ” 自动 缓冲区 地在内存中开辟 一个文件缓冲区, 程序 一个文件缓冲区 磁盘 在对文件进行处 数据区 文件 理时, 通过文件 输入 缓冲区进行输入 缓冲区 与输出。 与输出。
文本文件中的回车换行符在输入时转换为一个换行符, 文本文件中的回车换行符在输入时转换为一个换行符, 在输出时又转换为回车换行符。 在输出时又转换为回车换行符。 为了可靠地打开指定的文件, 常用下列方法: 为了可靠地打开指定的文件 常用下列方法: if ((fp=fopen(“file1” , “ r ”))==NULL) { printf(“cannot open this file\n”); exit(0); } 二、 文件关闭函数 fclose 使用完一个文件后必须关闭它, 使用完一个文件后必须关闭它 , 以防数据丢失和 被其它操作误用。 被其它操作误用。 fclose函数的调用格式: 函数的调用格式: 函数的调用格式 fclose(文件指针 文件指针); 文件指针 关闭文件时的操作就是使指针与文件“脱钩” 关闭文件时的操作就是使指针与文件“脱钩”。正常 关闭时fclose函数返回 值,关闭有误时返回非 值。 函数返回0值 关闭有误时返回非0值 关闭时 函数返回

C语言入门经典(第四版)10-基本输入和输出操作

C语言入门经典(第四版)10-基本输入和输出操作

C入门经典(第4版) 入门经典( 10.4.2 转义序列
在printf()和wprintf()函数的格式控制字符串中可以包含空白字符.空白字 printf()和wprintf()函数的格式控制字符串中可以包含空白字符.空白字 符有换行符,回车符,换页符,空格和制表符.它们用以\ 符有换行符,回车符,换页符,空格和制表符.它们用以\开头的转义序列表 示.
C入门经典(第4版) 入门经典( 10.3.2 输入格式控制字符串
在scanf()或wscanf()函数中使用的格式控制字符串不完全类似于printf()中 scanf()或wscanf()函数中使用的格式控制字符串不完全类似于printf()中 的格式控制字符串.在格式控制字符串中添加一个或多个空白字符,如空格' ', 的格式控制字符串.在格式控制字符串中添加一个或多个空白字符,如空格' ', 制表符' t'或换行符'n',scanf()会忽略空白字符,直接读入输入中的下一个非空 制表符'\t'或换行符'n',scanf()会忽略空白字符,直接读入输入中的下一个非空 白字符.在格式控制字符串中只要出现一个空白字符,就会造成无数个连续的 空白字符被忽略.因此,可以在格式字符串内加入任意多个的空白字符,使输 入易于理解.注意,scanf()默认忽略空白字符,但使用%c,%[]或%n指定符 入易于理解.注意,scanf()默认忽略空白字符,但使用%c,%[]或%n指定符 读取数据时除外 .
C入门经典(要使用scanf()函数从键盘输入数据,使用printf()函数将数据输 前面章节主要使用scanf()函数从键盘输入数据,使用printf()函数将数据输 出到屏幕上.事实上,使用这些函数指定从哪里输入或输出到哪里去的方式没 有什么特别.因为scanf()函数可以从任何地方接收信息,只要这些信息是字符 有什么特别.因为scanf()函数可以从任何地方接收信息,只要这些信息是字符 流即可.同样,printf()函数也可以将数据输出到任何能接收字符流的地方去. 流即可.同样,printf()函数也可以将数据输出到任何能接收字符流的地方去. 这并不是巧合:C 这并不是巧合:C语言的标准输入输出函数都是独立于设备的,程序员不需要 考虑如何在特定设备上传入传出数据.C 考虑如何在特定设备上传入传出数据.C语言的库函数和操作系统会确保在特 定设备上的操作完全正常.

第10章-编译预处理ppt课件(全)

第10章-编译预处理ppt课件(全)
#include <stdio.h> #define M(x,y,z) x*y+z int main( ) { int a=1,b=2, c=3; printf(“%d\n”,M(a+b,b+c,c+a)); return 0; }
-8-
带参数的宏定义(续)
【例10-3】用宏来定义多个语句的例子。
宏的使用有很多好处,不仅可以简化程序的书写,而且便于程序的 修改和移植,使用宏名来代替一个字符串,可以减少程序中重复书写某 些字符串的工作量。
根据宏定义中是否有参数,可以将宏分为不带参数的宏定义与带参 数的宏定义两种,下面分别讨论这两种宏的定义与调用。
-4-
10.1.1 不带参数的宏定义
不带参数的宏的宏名后面没有参数,不带参数的宏定义又称简单宏 定义。其定义的一般形式为:
宏定义是用一个标识符来表示一个字符串,这个字符串可以是常量、变量或表 达式。在宏替换时,用该字符串代换宏名。根据宏定义中是否有参数,可以将宏分 为不带参数的宏定义与带参数的宏定义两种。在写带有参数的宏定义时,宏名与带 括号参数间不能有空格,否则将空格以后的字符都作为了替换字符串的一部分,这 样就变成不带参数的宏定义了。不要把带参数的宏定义与带参数的函数混淆,带参 的宏定义在预处理时只是字符串的替换,而带参的函数却是将实参的值一一对应的 传递给形参。
#define 宏名 字符串 其中,“#”表示预处理命令。define是关键字,表示该命令为宏定 义。为了与一般的普通变量相区别,宏名一般使用大写。“字符串”一 般为常量、表达式或字符串。 在进行预处理时,系统会将程序中的“宏名”用“字符串”来替换。
-5-Biblioteka 10.1.1 不带参数的宏定义
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档