文章目录

  • C语言
    • 1.helloworld
      • 1.1 pause
      • 1.2 cls清屏
      • 1.3 加法运算
      • 1.4 hello
    • 2 常量变量和数据类型
      • 2.1 常量
      • 2.2 变量
      • 2.3 sizeof数据类型大小
      • 2.4 无符号整型
      • 2.5 字符类型
        • 2.5.1 字符类型简介
        • 2.5.2 字符类型运算
      • 2.6 实数型
      • 2.7 进制和转换
      • 2.8 数据溢出
    • 3. 运算符和分支循环语句
      • 3.1 字符串输出
        • 3.1.1 字符数组输出
        • 3.1.2 字符数组格式化输出(%-15s)
        • 3.1.2 字符输出 (putchar)
      • 3.2 格式化输入
        • 3.2.1 获取单个输入
        • 3.2.2 获取多个输入
        • 3.2.3 连续相同输入
        • 3.2.4 字符数组输入
        • 3.2.5 getchar获取字符输入
      • 3.3 算数运算符
        • 3.3.1 数学运算
        • 3.3.2 自增运算(a++与++a)
      • 3.4 逻辑运算
        • 3.5 三目运算
        • 3.5.1 三目运算使用
        • 3.5.2 三目运算案例2
      • 3.6 隐式转换和强制类型转换
        • 3.6.1 隐式转换
        • 3.6.2 强制类型转换
      • 3.7 if 语句
        • 3.7.1 if …else… 语句
        • 3.7.2 else if语句
        • 3.7.3 if else if else 语句
      • 3.8 switch
      • 3.9 while 循环
      • 3.10 do while
        • 3.10.1 do..whie语句
        • 3.10.2 水仙花数问题
    • 4. for循环和数组排序
      • 4.1 for 循环
        • 4.1.1 基础的for循环
        • 4.1.2 省略的表达式1
        • 4.1.3 省略表达式2
        • 4.1.4 省略表达式123
        • 4.1.5 多个表达式
      • 4.2 猜数字游戏
      • 4.3 模拟电子表
      • 4.4 9*9乘法表
        • 4.4.1 正序9*9乘法表
        • 4.4.2 倒序9*9乘法表
      • 4.5 continue
        • 4.5.1 for 循环中continue
        • 4.5.2 while循环中continue
      • 4.6 goto语句
        • 4.6.1 goto语句简介
        • 4.6.2 循环语句中goto
      • 4.7 数组
        • 4.7.1 数组简介
        • 4.7.2 数组大小与个数
        • 4.7.3 数组初始化
      • 4.8 数组逆序
      • 4.9 冒泡排序
    • 5. 二维数组、字符串、函数
      • 5.1 二维数组
        • 5.1.1 二维数组定义
        • 5.1.2 二维数组练习
        • 5.1.3 多维数组
      • 5.2 字符数组和字符串
        • 5.2.1 字符数组打印
        • 5.2.2 统计字符串中每个字符出现的次数
        • 5.2.3 scanf获取字符串
      • 5.3 字符串操作函数
        • 5.3.1 gets 函数
        • 5.3.2 fgets 函数
        • 5.3.3 puts 函数
        • 5.3.4 fputs函数
        • 5.3.5 strlen函数
        • 5.3.6 实现strlen函数
      • 5.4 字符串拼接
      • 5.5 函数
        • 5.5.1 函数声明定义
        • 5.5.2 exit函数
    • 6. 指针
      • 6.1 指针基础
    • 6.2 野指针和空指针
        • 6.2.1 野指针
        • 6.2.2 空指针
      • 6.3 泛型指针(void **)
      • 6.4 const作用
        • 6.4.1 修饰变量
        • 6.4.2 const int *p
        • 6.4.3 int const *p
        • 6.4.4 int * const p
        • 6.4.5 const int * const p
      • 6.5 指针和数组
        • 6.5.1 数组操作
        • 6.5.2 指针操作
      • 6.6 指针算法运算
        • 6.6.1 指针+-操作
        • 6.6.2 &数组名 +1操作
        • 6.6.3 指针加减操作
        • 6.6.4 strlen实现方式(指针)
      • 6.7 指针的比较运算
      • 6.8 指针数组
      • 6.9 多级指针
    • 7. 指针和字符串
      • 7.1 传值和传址
      • 7.2 数组做函数参数
      • 7.3 指针做函数返回值
      • 7.4 指针和字符串
      • 7.5 字符串比较
      • 7.6 字符串拷贝
      • 7.7 字符串中查找字符
      • 7.8 字符串去除空格
      • 7.9 字符串中找子串
    • 8. 字符串和内存
      • 8.1 字符串操作
        • 8.1.1 非空字符串元素个数
        • 8.1.2 字符串逆序
        • 8.1.3 字符串拷贝
        • 8.1.4 字符串拼接strcat和strncat
        • 8.1.5 strcmp和strcmpz
        • 8.1.6 格式化度读入和写出sprintf,sscanf
        • 8.1.7 字符串分割strtok
        • 8.1.8 atol_atof_atol
      • 8.2 局部变量
      • 8.3 申请堆空间
      • 8.4 二级指针malloc空间
    • 9. 文件
      • 9.1 联合体和枚举
        • 9.1.1 联合体
        • 9.1.2 枚举
      • 9.2 系统文件
      • 9.3 文件的打开和关闭
      • 9.4 fput和fget
      • 9.5 feof
      • 9.6 fgets和fputs
      • 9.7 四则运算(文件版)
      • 9.8 sprintf和sscanf
      • 9.9 文件版随机排序
      • 9.10 结构化读取fwrite和fread
      • 9.11 大文件拷贝
      • 9.12 随机读取文件
        • 9.12.1 fseek
        • 9.12.2 改变指针位置
        • 9.12.3 读取指针位置
      • 9.13 获取文件属性
      • 9.14 缓存区刷新

C语言

1.helloworld

1.1 pause

#include // #:关键标识符,表示引入头文件; include:引入头文件关键字// stdio.h: 系统标准输入、输出库对应的头文件。 给printf函数服务。//: 使用系统库函数。"": 用户自定义库函数。int main(void)//int: 函数返回值是整型数据。mian:函数名。程序唯一的入口。必须有,且只有一次。 void:函数调用无需传参。{// 函数体 起始位置printf("hello world!\n");// 将 “hellow world”写到屏幕上。 \n: 回车换行system("pause");// 调用 syatem 函数, 实现暂停功能。 return 0;// 返回当前函数调用 --- 退出程序。0 要跟 main函数的返回值一一对应。}// 函数体 结束位置

1.2 cls清屏

#include #include int main(void)//int: 函数返回值是整型数据。mian:函数名。程序唯一的入口。必须有,且只有一次。 void:函数调用无需传参。{// 函数体 起始位置printf("-----------hello world!\n");// 将 “hellow world”写到屏幕上。 \n: 回车换行Sleep(2000);system("cls");// 调用 syatem 函数, 实现清屏功能。 return 0;// 返回当前函数调用 --- 退出程序。0 要跟 main函数的返回值一一对应。}

1.3 加法运算

#include int main(void){int a = 3;// 定义 整型变量 a, 赋初值 3int b = 5;// 定义 整型变量 b, 赋初值 5int c;// 定义 整型变量 c, 无初值c = a + b;// a + b ,将结果 赋值 给 cprintf("hello world\n");// %d:格式匹配符,匹配整型数据printf("%d\n", c /*注释*/ );printf("c = %d\n", c);printf("%d + %d = %d\n", a, b, c);printf("%d + %d = &d\n", a, b, a+b);return 0;}

1.4 hello

#include #define PI 3.14// 定义常量 PI宏定义// 我是一个单行注释君int main(void){printf("helloworld\n");/*wo shi 一个多行注释 sir*/printf("%d\n, PI");return 0;}

2 常量变量和数据类型

2.1 常量

#include #define PI 3.1415// 常量int main(void){// 圆的面积s = PI x 半径的平方// 圆的周长l = 2 * PI * r//int r = 3;// 变量const int r = 3;// 只读变量float s = PI * r * r;float l = 2 * PI * r;//printf("圆的周长为:%f\n", l);//18.849001//printf("圆的面积为:%f\n", s);//28.273500printf("圆的周长为:%.2f\n", l);// 指定小数点后保留2位, 对第3位做,4舍五入printf("圆的面积为:%.2f\n", s);// 指定小数点后保留2位return 0;}

2.2 变量

#include int main(void){int a; // 显示的做变量a的声明a = 56;// 变量使用、变量赋值。return 0;}

2.3 sizeof数据类型大小

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #define N 1024// 定义常量int main(void){int a = 10;// 定义变量: 三要素short b = 20;long c = 30;long long d = 40;printf("================a===%d\n", sizeof(a));printf("================b===%d\n", sizeof(b));printf("================c===%d\n", sizeof(c));printf("================d===%d\n", sizeof(d));printf("int 大小为:%d\n", sizeof(int));printf("short 大小为:%d\n", sizeof(short));printf("long 大小为:%d\n", sizeof(long));printf("long long 大小为:%d\n", sizeof(long long));system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #define N 1024// 定义常量int main(void){int a = 3;short b = 4;long c = 5;// 5L5llong long d = 6;// 5LL5llprintf("sizeof(int) = %d\n", sizeof(int));printf("sizeof(short) = %d\n", sizeof(short));printf("sizeof(long) = %d\n", sizeof(long));printf("sizeof(long long) = %d\n", sizeof(long long));printf("--------------------------------------\n");unsigned int aun = 3;// 3uunsigned short bun = 4;// 3uunsigned long cun = 5;// 3luunsigned long long dun = 6;// 3lluprintf("sizeof(unsigned int) = %d\n", sizeof(unsigned int)); // aunprintf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));printf("sizeof(unsigned long long) = %d\n", sizeof(unsigned long long));}

2.4 无符号整型

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){size_t var = 10;printf("var = %u\n", var);unsigned int a = 10u;// 简写成 unsigned int a = 10;unsigned short b = 20u;// 简写成 unsigned short b = 20;unsigned long c = 30Lu; unsigned long long d = 40LLu;printf("unsigned int 型数据值:%u\n", a);printf("unsigned short 型数据值:%hu\n", b);printf("unsigned long 型数据值:%lu\n", c);printf("unsigned long long 型数据值:%llu\n", d);system("pause");return EXIT_SUCCESS;}

2.5 字符类型

2.5.1 字符类型简介

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char ch = 'A';// 65//printf("1 ch = %c\n", ch);printf("1 ch = %d\n", ch);ch = 'm'; ////printf("2 ch = %c\n", ch);printf("2 ch = %d\n", ch);//ch = 97;ch = 'a';// 97//printf("3 ch = %c\n", ch);printf("3 ch = %d\n", ch);system("pause");return EXIT_SUCCESS;}

2.5.2 字符类型运算

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // A -- 65 a -- 97 int main(void){char ch = 'M';char var = '5';printf("ch = %c\n", ch + 32);printf("var = %c\n", var + 4);printf("'\\n\'的值为=%d\n", '\n');system("pause");return EXIT_SUCCESS;}

2.6 实数型

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){float m = 3.145;double n = 4.566545;printf("m = %08.2f\n", m);printf("n = %08.3lf\n", n);system("pause");return EXIT_SUCCESS;}

2.7 进制和转换

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 0x2C;// 等价 0x2c printf("10进制显示 a = %d\n", a);printf("8进制显示 a = %o\n", a);printf("16进制显示 a = %x\n", a);system("pause");return EXIT_SUCCESS;}

2.8 数据溢出

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char ch = 127+1;// -128 -- 127printf("ch = %d\n", ch);system("pause");return EXIT_SUCCESS;}

3. 运算符和分支循环语句

3.1 字符串输出

3.1.1 字符数组输出

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char ch = 'a';printf("ch = %c\n", ch);char str[20] = "hello world";printf("str = %s\n", str);system("pause");return EXIT_SUCCESS;}

3.1.2 字符数组格式化输出(%-15s)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[] = "hello world";printf("str = |%-15s|\n", str);system("pause");return EXIT_SUCCESS;}

3.1.2 字符输出 (putchar)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){putchar(97);// 'a' == 97putchar('b');putchar('c');putchar('d');putchar('abcZ');system("pause");return EXIT_SUCCESS;}

3.2 格式化输入

3.2.1 获取单个输入

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 获取用户输入 整数int main(void){int a;scanf("%d", &a);// &:表示取出变量a的地址。描述a的空间printf("a = %d\n", a);system("pause");return EXIT_SUCCESS;}

3.2.2 获取多个输入

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 获取用户输入 整数int main(void){char ch1, ch2, ch3;//连续定义同类型多个变量。scanf("%c%c%c", &ch1, &ch2, &ch3);// &:表示取出变量ch的地址。描述a的空间printf("ch1 = %c\n", ch1);printf("ch2 = %c\n", ch2);printf("ch3 = %c\n", ch3);system("pause");return EXIT_SUCCESS;}

3.2.3 连续相同输入

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 获取用户输入 整数int main(void){int a1, a2, a3;//连续定义同类型多个变量。scanf("%d %d %d", &a1, &a2, &a3);// &:表示取出变量ch的地址。描述a的空间printf("a1 = %d\n", a1);printf("a2 = %d\n", a2);printf("a3 = %d\n", a3);system("pause");return EXIT_SUCCESS;}

3.2.4 字符数组输入

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 获取用户输入 整数int main(void){char a[5];// 大小为5字节的数组scanf("%s", a);// 接收用户键盘输入,写入数组a中printf("a = %s\n", a);system("pause");return EXIT_SUCCESS;}

3.2.5 getchar获取字符输入

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 获取用户输入 整数int main(void){char ch;ch = getchar();// 接收用户输入,返回接收到的ASCII码printf("ch = %c\n", ch);putchar(ch);putchar('\n');system("pause");return EXIT_SUCCESS;}

3.3 算数运算符

3.3.1 数学运算

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int b = 20;int c = a * b;int d = 34 / 10;// 0.5//int m = 98 / 0;printf("d = %d\n", d);system("pause");return EXIT_SUCCESS;}

3.3.2 自增运算(a++与++a)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int b = 50;printf("a = %d\n", a++);// 先取值给%d, 在自增printf("----a = %d", a);printf("b = %d\n", ++b);// 先自增,再取值。 printf("----b = %d\n", b);system("pause");return EXIT_SUCCESS;}

3.4 逻辑运算

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 34;int b = 0;char str[10] = "hello";++str[0];printf("a = %d\n", !a);printf("b = %d\n", !b);printf("======%d\n", a && !b);printf("------%d\n", !a || b);system("pause");return EXIT_SUCCESS;}

3.5 三目运算

3.5.1 三目运算使用

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // int main(void){int a = 40;int b = 4;int m = a  b ? 69 : 100);system("pause");return EXIT_SUCCESS;}

3.5.2 三目运算案例2

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // int main(void){int a = 10, b = 20, c = 30;int x = (a = 1, c = 5, b = 2);printf("x = %d\n", x);printf("a = %d\n", a);printf("b = %d\n", b);printf("c = %d\n", c);system("pause");return EXIT_SUCCESS;}

3.6 隐式转换和强制类型转换

3.6.1 隐式转换

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 隐式类型转换。int main(void){int a = 321;char ch = a;printf("ch = %d\n", ch);// 字符类型转换为整数类型system("pause");return EXIT_SUCCESS;}

3.6.2 强制类型转换

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 强制类型转换int main(int var){//int *p = (int *)malloc(100);float price = 3.6;int weight = 4;//double sum = (int)price * weight;double sum = (int)(price * weight);printf("价格:%lf\n", sum);system("pause");return EXIT_SUCCESS;}

3.7 if 语句

3.7.1 if …else… 语句

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a;scanf("%d", &a);if (a > 0){printf("a > 0\n");}else{printf("a <= 0\n");}system("pause");return EXIT_SUCCESS;}

3.7.2 else if语句

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int score;// 100--90 优 90 -- 70 良好 70 -- 60 及格< 60 差劲printf("请输入学生成绩:");scanf("%d", &score);if (score >= 90 && score <= 100){printf("优秀\n");}else if (score < 90 && score >= 70){printf("良好\n");}else if (score < 70 && score >= 60){printf("及格\n");}else{printf("不及格\n");}system("pause");return EXIT_SUCCESS;}

3.7.3 if else if else 语句

int main0703(void){int pig1, pig2, pig3;// if (pig1 > pig2 && pig1 > pig3)// pig1 > pig2 " />printf("请输入三只小猪的体重:");scanf("%d %d %d", &pig1, &pig2, &pig3);if (pig1 > pig2)// 满足,说明pig1最重{if (pig1 > pig3){printf("第一只小猪最重,体重为:%d\n", pig1);}else{printf("第3只小猪最重,体重为:%d\n", pig3);}} else{if (pig2 > pig3){printf("第2只小猪最重,体重为:%d\n", pig2);}else{printf("第3只小猪最重,体重为:%d\n", pig3);}}system("pause");return EXIT_SUCCESS;}

3.8 switch

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int score;scanf("%d", &score);switch (score / 10){case 10:// 100 -- 90 优秀case 9:printf("优秀\n");break;case 8:// 70 -- 90 良好case 7:printf("良好\n");//break;case 6: // 70 - 60 及格printf("及格\n");//break;default:printf("不及格\n");break;}system("pause");return EXIT_SUCCESS;}

3.9 while 循环

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // int main(void){int num = 1;while (num <= 100){if ((num % 7 == 0) || (num % 10 == 7) || (num / 10 == 7))// 个位、10位、7的倍数{printf("敲桌子\n");}else{printf("%d\n", num);}num++;// 递增}system("pause");return EXIT_SUCCESS;}

3.10 do while

3.10.1 do…whie语句

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // while 的基础用法int main(void){int a = 1;do{a++;printf("a = %d\n", a);} while (a < 10);system("pause");return EXIT_SUCCESS;}

3.10.2 水仙花数问题

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 水仙花数:一个三位数。各个位上的数字的立方和等于本数字。 int main(void){int a, b, c;int num = 100;do {a = num % 10;// 个位b = num / 10 % 10;// 十位c = num / 100;// 百位if (a*a*a + b*b*b + c*c*c == num){printf("%d\n", num);}num++;} while (num < 1000);system("pause");return EXIT_SUCCESS;}

4. for循环和数组排序

4.1 for 循环

4.1.1 基础的for循环

#define _CRT_SECURE_NO_WARNINGS#include #include #include int main(void){srand(time(NULL));//种随机数种子。int n = 0;int num = rand() % 100;// 生成随机数for (;;)// while(1){printf("请输入猜测的数字:");scanf("%d", &n);if (n < num){// for、while、if 如果执行语句只有一条。 { } 可以省略printf("猜小了\n");}else if (n > num)printf("猜大了\n");else{printf("猜中!!!\n");break;// 跳出}}printf("本尊是:%d\n", num);system("pause");return 0;}

4.1.2 省略的表达式1

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 基础for循环int main(void){int i = 1;// 循环因子int sum = 0;for (; i <= 100; i++){sum = sum + i;//sum += i;}printf("sum = %d\n", sum);system("pause");return EXIT_SUCCESS;}

4.1.3 省略表达式2

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 基础for循环int main(void){int i = 1;// 循环因子int sum = 0;for (; i <= 100; ){sum = sum + i;//sum += i;i++;}printf("sum = %d\n", sum);system("pause");return EXIT_SUCCESS;}

4.1.4 省略表达式123

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 省略表达式123,无限循环int main(void){int i = 0;// 循环因子//for (;1;)// 死循环。while(k=1)for (;;){printf("i = %d\n", i);i++;}system("pause");return EXIT_SUCCESS;}

4.1.5 多个表达式

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 表达式有多个int main(void){int i = 0;int a = 0;for (i = 1, a = 3; a < 20; i++){printf("i = %d\n", i);printf("a = %d\n", a);a += 5;}system("pause");return EXIT_SUCCESS;}

4.2 猜数字游戏

#define _CRT_SECURE_NO_WARNINGS#include #include #include int main(void){srand(time(NULL));//种随机数种子。int n = 0;int num = rand() % 100;// 生成随机数for (;;)// while(1){printf("请输入猜测的数字:");scanf("%d", &n);if (n < num){// for、while、if 如果执行语句只有一条。 { } 可以省略printf("猜小了\n");}else if (n > num)printf("猜大了\n");else{printf("猜中!!!\n");break;// 跳出}}printf("本尊是:%d\n", num);system("pause");return 0;}

4.3 模拟电子表

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #include int main(void){int i, j, k;// 小时for (i = 0; i < 24; i++){// 分钟for (j = 0; j < 60; j++){// 秒for (k = 0; k < 60; k++){printf("%02d:%02d:%02d\n", i, j, k);Sleep(960);system("cls");// 清屏}}}system("pause");return EXIT_SUCCESS;}

4.4 9*9乘法表

4.4.1 正序9*9乘法表

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 正序99乘法表int main(void){for (size_t i = 1; i <= 9; i++){for (size_t j = 1; j <= i; j++){printf("%dx%d=%d\t", j, i, j * i);}printf("\n");}system("pause");return EXIT_SUCCESS;}

4.4.2 倒序9*9乘法表

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 倒序 99 乘法表int main(void){int i, j;for (i = 9; i >= 1; i--)// 行{for (j = 1; j <= i; j++)// 列{printf("%dx%d=%d\t", j, i, j * i);}putchar('\n');}system("pause");return EXIT_SUCCESS;}

4.5 continue

4.5.1 for 循环中continue

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){for (size_t i = 0; i < 5; i++){if (i == 3){continue;}printf("i = %d\n", i);printf("============1=========\n");printf("============2=========\n");printf("=============3========\n");printf("============4=========\n");printf("=============5========\n");}system("pause");return EXIT_SUCCESS;}

4.5.2 while循环中continue

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int num = 5;//while (num--)// 当num自减为 0 时循环终止。等价于 while (num-- != 0)while (num-- != 0) // 当num自减为 0 时循环终止。{printf("num = %d\n", num);if (num == 3){continue;}printf("============1=========\n");printf("============2=========\n");printf("=============3========\n");printf("============4=========\n");printf("=============5========\n");}system("pause");return EXIT_SUCCESS;}

4.6 goto语句

4.6.1 goto语句简介

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){printf("============1==========\n");printf("============2==========\n");goto LABLE;printf("============3==========\n");printf("============4==========\n");printf("============5==========\n");printf("============6==========\n");printf("============7==========\n");LABLE:printf("============8==========\n");printf("============9==========\n");printf("============10==========\n");system("pause");return EXIT_SUCCESS;}

4.6.2 循环语句中goto

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int i = 0;for (i = 0; i < 10; i++){if (i == 5)goto ABX234;printf("i = %d\n", i);}for (int j = 0; j < 20; j++){ABX234:printf("j = %d\n", j);}system("pause");return 0;}

4.7 数组

4.7.1 数组简介

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 5, b = 29, c = 10;int arr[10] = { 1, 2 ,4, 6, 76, 8, 90 ,4, 3, 6 };//int a = 109;printf("&arr[0] = %p\n", &arr[0]);// 取数组首元素的地址printf("arr = %p\n", arr);// 数组名system("pause");return EXIT_SUCCESS;}

4.7.2 数组大小与个数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 5, b = 29, c = 10;int arr[12] = { 1, 2 ,4, 6, 76, 8, 90 ,4, 3, 6 , 6, 8 };//int a = 109;printf("数组大小:%u\n", sizeof(arr));printf("数组元素的大小:%u\n", sizeof(arr[0]));printf("数组元素个数:%d\n", sizeof(arr) / sizeof(arr[0]));system("pause");return EXIT_SUCCESS;}

4.7.3 数组初始化

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 数组初始化int main(void){int arr[10];//int a = 109;arr[0] = 5;arr[1] = 6;arr[2] = 7;int n = sizeof(arr) / sizeof(arr[0]);for (size_t i = 0; i < n; i++){printf("%d\n", arr[i]);}system("pause");return EXIT_SUCCESS;}

4.8 数组逆序

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 数组元素逆序int main(void){int arr[] = { 1, 6, 8, 0, 4, 3, 9, 2 };// {2, 9, 3, 4, 0, 8, 6, 1}int len = sizeof(arr) / sizeof(arr[0]); //数组元素个数int i = 0;// i表示数组的首元素下标int j = len - 1;// 表示数组的最后一个元素下标int temp = 0;// 临时变量 // 交换 数组元素,做逆序while (i < j){temp = arr[i];// 三杯水法变量交换arr[i] = arr[j];arr[j] = temp;i++;j--;}// 打印交互后的 数组for (size_t n = 0; n < len; n++){printf("%d ", arr[n]);}printf("\n");system("pause");return EXIT_SUCCESS;}

4.9 冒泡排序

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int arr[] = { 12, 32, 14, 62, 27, 8, 89 };int n = sizeof(arr) / sizeof(arr[0]);// 数组元素个数int temp = 0;// 临时变量for (size_t i = 0; i < n; i++){printf("%d ", arr[i]);}printf("\n");// 完成乱序数组的冒泡排序。for (size_t i = 0; i < n - 1; i++)// 外层控制行{for (size_t j = 0; j < n - 1 - i; j++)// 内层控制列{if (arr[j] > arr[j + 1])// 满足条件 三杯水交换{temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}// 打印排序后的数组,确定正确性。for (size_t i = 0; i < n; i++){printf("%d ",arr[i]);}printf("\n");system("pause");return EXIT_SUCCESS;}

5. 二维数组、字符串、函数

5.1 二维数组

5.1.1 二维数组定义

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int arr[3][4] = { {2, 7, 8, 5},{75, 8, 9, 8},{26, 37, 99, 9} };for (size_t i = 0; i < 3; i++)//行{for (size_t j = 0; j < 4; j++)//列{printf("%d ", arr[i][j]);}printf("\n");}printf("数组的大小为:%u\n", sizeof(arr));printf("数组行的大小:%u\n", sizeof(arr[0]));printf("数组一个元素的大小:%u\n", sizeof(arr[0][0]));printf("行数=总大小/一行大小:%d\n", sizeof(arr) / sizeof(arr[0]));printf("列数=行大小/一个元素大小:%d\n", sizeof(arr[0]) / sizeof(arr[0][0]));printf("arr= %p\n", arr);printf("&arr[0] = %p\n", &arr[0][0]);printf("arr[0] = %p\n", arr[0]);system("pause");return EXIT_SUCCESS;}

5.1.2 二维数组练习

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){/*56 78 9245 67 9329 83 8893 56 8972 83 81*/int scores[5][3] = { 1, 2, 4, 5, 6, 7, 8, 9 };int row = sizeof(scores) / sizeof(scores[0]);int col = sizeof(scores[0]) / sizeof(scores[0][0]);// 获取 5 名学生、3门功课成绩for (size_t i = 0; i < row; i++){for (size_t j = 0; j < col; j++){scanf("%c", &scores[i][j]);}}// 求一个学生的总成绩for (size_t i = 0; i < row; i++) // 每个学生{int sum = 0;for (size_t j = 0; j < col; j++)// 每个学生的成绩{sum += scores[i][j];}printf("第%d个学生的总成绩为:%d\n", i + 1, sum);}//求一门功课的总成绩for (size_t i = 0; i < col; i++)// 第几门功课{int sum = 0;for (size_t j = 0; j < row; j++)// 每门功课的第几个学生{sum += scores[j][i];}printf("第%d门功课的总成绩为:%d\n", i + 1, sum);}system("pause");return EXIT_SUCCESS;}

5.1.3 多维数组

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a[3][4][2] ={{{1, 2},{2, 3},{4, 5},{5, 6}},{{45, 67},{78, 90},{12, 6},{45, 9}},{{ 45, 67 },{ 78, 90 },{ 12, 6 },{ 45, 9 }}};//int arr[2][3][5] = {1, 2, 4, 5, 6, 7, 8 , 9, 0, 0, 7, 9, 8};for (size_t i = 0; i < 3; i++){for (size_t j = 0; j < 4; j++){for (size_t k = 0; k < 2; k++){printf("%d ", a[i][j][k]);}printf("\n");}printf("\n\n");}system("pause");return EXIT_SUCCESS;}

5.2 字符数组和字符串

5.2.1 字符数组打印

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };char str2[] = "world";//== {'w', 'o', 'r', 'l', 'd', '\0'}printf("%s\n", str2);system("pause");return EXIT_SUCCESS;}

5.2.2 统计字符串中每个字符出现的次数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[11] = { 0 };// helloworld -->26个英文字母 a-za:97 d:100// scanf("%s", str);for (size_t i = 0; i < 10; i++){scanf("%c", &str[i]);}int count[26] = { 0 };// 代表26个英文字母出现的次数。 for (size_t i = 0; i < 11; i++){int index = str[i] - 'a';// 用户输入的字符在 count数组中的下标值。count[index]++;}for (size_t i = 0; i < 26; i++){if (count[i] != 0){printf("%c字符在字符串中出现 %d 次\n", i + 'a', count[i]);}}system("pause");return EXIT_SUCCESS;}

5.2.3 scanf获取字符串

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[100];//scanf("%s", str);scanf("%[^\n]s", str);printf("%s\n", str);system("pause");return EXIT_SUCCESS;}

5.3 字符串操作函数

5.3.1 gets 函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //getsint main(void){char str[10];printf("请输入字符串:");gets(str);printf("%s\n", str);system("pause");return EXIT_SUCCESS;}

5.3.2 fgets 函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[10];printf("获取的字符串为:%s", fgets(str, sizeof(str), stdin));system("pause");return EXIT_SUCCESS;}

5.3.3 puts 函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //putsint main(void){char str[] = "hello world\n";int ret = puts(str);// puts("hello world");printf("ret = %d\n", ret);system("pause");return EXIT_SUCCESS;}

5.3.4 fputs函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //fputsint main(void){char str[] = "hello world\n";//int ret = fputs(str, stdout);// int ret = fputs("hello world\n", stdout);printf("ret = %d\n", ret);system("pause");return EXIT_SUCCESS;}

5.3.5 strlen函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //fputs// strlen() 函数 : 获取字符串的 有效长度: 不包含\0int main(void){char str[] = "hello\0world";printf("sizeof(str) = %u\n", sizeof(str));printf("strlen(str) = %u\n", strlen(str));system("pause");return EXIT_SUCCESS;}

5.3.6 实现strlen函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //fputsint main(void){char str[] = "hello world";int i = 0;while (str[i] != '\0'){i++;}printf("%d\n", i);// 等价于 printf("%d\n", strlen(str));system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 进阶版int main(void){char str[] = "hello\0world";int i = 0;while (str[i++]);printf("%d\n", i - 1);// 等价于 printf("%d\n", strlen(str));system("pause");return EXIT_SUCCESS;}

5.4 字符串拼接

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str1[] = "hello";// [ h e l l o \0 ]char str2[] = "world";char str3[100];int i = 0;// 循环 str1while (str1[i] != '\0') // '\0' != '\0'{str3[i] = str1[i];// 循环着将str1中的每一个元素,交给str3i++;}// str3=[h e l l o]//printf("%d\n", i);--> 5int j = 0;// 循环 str2while (str2[j]) // 等价于 while(str2[j] !='\0') 等价于 while(str2[j] != 0){str3[i + j] = str2[j];j++;}// str3=[h e l l o w o r l d]// 手动添加 \0 字符串结束标记str3[i + j] = '\0';printf("str3 = %s\n", str3);system("pause");return EXIT_SUCCESS;}

5.5 函数

5.5.1 函数声明定义

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void bubble_sort(int arr[]);// 函数声明 int arr[] 函数形参void print_arr(int arr[]);int main(void){printf("add = %d\n", add(2, 6));int arr[] = { 54, 5, 16, 34 , 6, 9, 34, 1, 7, 93 };bubble_sort(arr);print_arr(arr);system("pause");return EXIT_SUCCESS; // 底层 调用 _exit(); 做退出}void print_arr(int arr[]){for (size_t i = 0; i < 10; i++){printf("%d ", arr[i]);}}void bubble_sort(int arr[]){int i, j, temp;for (i = 0; i < 10 - 1; i++){for (j = 0; j < 10 - 1 - i; j++){if (arr[j] < arr[j + 1]){temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}int add(int a, int b){return a + b;}

5.5.2 exit函数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int func(int a, char ch);int main(void){int ret = func(10, 'a');printf("ret = %d\n", ret);system("pause");//return EXIT_SUCCESS;exit(EXIT_SUCCESS);}int func(int a, char ch){printf("a = %d\n", a);printf("ch = %c\n", ch);//return 10;exit(10);}

6. 指针

6.1 指针基础

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int* p = &a;//*p = 2000;a = 350;//printf("a = %d\n", a);printf("*p = %d\n", *p);printf("sizeof(int *) = %u\n", sizeof(int*));printf("sizeof(short *) = %u\n", sizeof(short*));printf("sizeof(char *) = %u\n", sizeof(char*));printf("sizeof(long *) = %u\n", sizeof(long*));printf("sizeof(double *) = %u\n", sizeof(double*));printf("sizeof(void *) = %u\n", sizeof(void*));system("pause");return EXIT_SUCCESS;}

6.2 野指针和空指针

6.2.1 野指针

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 野指针1int main(void){int *p;*p = 2000;printf("*p = %d\n", *p);system("pause");return EXIT_SUCCESS;}
#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 野指针2int main(void){int m;//int *p = 1000; // 0-255 确定留给操作系统int *p = 0x0bfcde0000;p = &m;*p = 2000;printf("*p = %d\n", *p);system("pause");return EXIT_SUCCESS;}

6.2.2 空指针

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 空指针int main(void){int *p = NULL; // NULL == 0// .....// lllll// .....if (p != NULL){*p = 300;printf("*p = %d\n", *p);}system("pause");return EXIT_SUCCESS;}

6.3 泛型指针(void **)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 345;char ch = 'R';void* p;// 万能指针、泛型指针//p = &a;p = &ch;printf("%c\n", *(char*)p);system("pause");return EXIT_SUCCESS;}

6.4 const作用

6.4.1 修饰变量

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 修饰变量int main(void){const int a = 20;int* p = &a;*p = 650;printf("%d\n", a);system("pause");return EXIT_SUCCESS;}

6.4.2 const int *p

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // const int *p;int main(void){int a = 10;int b = 30;const int* p = &a;//*p = 500;p = &b;printf("*p=%d", *p);system("pause");return EXIT_SUCCESS;}

6.4.3 int const *p

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int b = 30;int const* p = &a;//*p = 300;p = &b;printf("*p=%d", *p);system("pause");return EXIT_SUCCESS;}

6.4.4 int * const p

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int b = 30;int* const p = &a;*p = 300;//p = &b;printf("a = %d\n", *p);printf("a = %d\n", a);system("pause");return EXIT_SUCCESS;}

6.4.5 const int * const p

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int b = 30;const int* const p = &a;//*p = 300;//p = &b;printf("a = %d\n", *p);printf("a = %d\n", a);system("pause");return EXIT_SUCCESS;}

6.5 指针和数组

6.5.1 数组操作

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 操作数组元素的 4 种方法int main(void){int a[] = { 1, 2, 4, 5, 6, 7, 8, 9, 0 };int n = sizeof(a) / sizeof(a[0]);int* p = a;printf("sizeof(a) = %u\n", sizeof(a));printf("sizeof(p) = %u\n", sizeof(p));for (size_t i = 0; i < n; i++){//printf("%d "), a[i];//printf("%d ", *(a+i));// a[i] == *(a+i)//printf("%d ", p[i]);printf("%d ", *(p + i));// p[i] = *(p+i)}printf("\n");system("pause");return EXIT_SUCCESS;}

6.5.2 指针操作

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 使用指针++操作数组元素int main(void){int arr[] = { 1, 2, 4, 5, 6, 7, 8, 9, 0 };int* p = arr;int n = sizeof(arr) / sizeof(arr[0]);printf("first p = %p\n", p);for (size_t i = 0; i < n; i++){printf("%d ", *p);p++;// p = p+1; 一次加过一个int大小。 一个元素。}putchar('\n');printf("last p = %p\n", p);system("pause");return EXIT_SUCCESS;}

6.6 指针算法运算

6.6.1 指针±操作

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 指针在数组中 +- 整数int main(void){int a[] = { 1, 2, 4, 5, 6, 7, 8, 9, 0 };//int *p = a; // a == &a[0];int* p = &a[5];printf("p-2 = %p\n", p - 2);printf("&a[3] = %p\n", &a[3]);system("pause");return EXIT_SUCCESS;}

6.6.2 &数组名 +1操作

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // &数组名 +1int main(void){short a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };printf("a = %p\n", a);printf("&a[0] = %p\n", &a[0]);printf("a+1 = %p\n", a + 1);printf("&a = %p\n", &a);printf("&a+1 = %p\n", &a + 1);system("pause");return EXIT_SUCCESS;}

6.6.3 指针加减操作

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 指针加减指针int main(void){int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };int* p = a;for (size_t i = 0; i < 10; i++){printf("%d ", *p);p++;}printf("p - a = %d\n", p - a);system("pause");return EXIT_SUCCESS;}

6.6.4 strlen实现方式(指针)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int mystrlen(char arr[]);int main(void){char abc[] = "hello world";int ret = mystrlen2(abc);// 实际参数 abcprintf("ret = %d\n", ret);system("pause");return EXIT_SUCCESS;}// 借助数组 实现 int mystrlen(char str[]){int i = 0;while (str[i] != '\0'){i++;}return i;}// 借助指针++ 实现 int mystrlen2(char str[]){char* p = str;while (*p != '\0'){p++;}return p - str;// 返回数组元素的个数。}

6.7 指针的比较运算

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a[] = { 1, 2, 4, 5, 6, 7, 8, 9, 0 };int *p = &a[0];if (p > a)printf("成立\n");else if (p < a)printf("不成立\n");elseprintf("== \n");system("pause");return EXIT_SUCCESS;}

6.8 指针数组

#include #include #include #include // 指针数组1int main(void){int a = 10;int b = 20;int c = 30;int* p1 = &a;int* p2 = &b;int* p3 = &c;int* arr[] = { p1, p2, p3 };// 整型指针数组arr, 存的都是整型地址。printf("*(arr[0]) = %d\n", *(*(arr + 0)));//arr[0] ==*(arr+0)printf("*(arr[0]) = %d\n", **arr);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // 指针数组2int main(void){int a[] = { 10 };int b[] = { 20 };int c[] = { 30 };int* arr[] = { a, b, c };// 整型指针数组arr, 存的都是整型地址。printf("*(arr[0]) = %d\n", *(*(arr + 0)));//arr[0] ==*(arr+0)printf("*(arr[0]) = %d\n", **arr);system("pause");return EXIT_SUCCESS;}

6.9 多级指针

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int a = 10;int* p = &a;int** pp = &p;// int **pp = &(&a); 不允许!!int*** ppp = &pp;printf("***ppp = %d\n", ***ppp);printf("**pp = %d\n", **pp);printf("*p = %d\n", *p);printf("a = %d\n", a);system("pause");return EXIT_SUCCESS;}

7. 指针和字符串

7.1 传值和传址

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int swap(int, int);// 函数声明int swap2(int*, int*);int main(void){int m = 23;int n = 57;printf("--before-- m = %d, n = %d\n", m, n);// 函数调用//swap(m, n);// m/n 实参swap2(&m, &n);printf("--after-- m = %d, n = %d\n", m, n);system("pause");return EXIT_SUCCESS;}int swap2(int* a, int* b)// 形参a、b, 需传地址值{int tmp = 0;tmp = *a;*a = *b;*b = tmp;return 0;}// 函数定义int swap(int a, int b)// a/b 形参{int tmp = 0;tmp = a;a = b;b = tmp;return 0;}

7.2 数组做函数参数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include //void BubbleSort(int arr[])// void BubbleSort(int *arr)void BubbleSort(int* arr, int n){for (int i = 0; i < n - 1; i++){for (int j = 0; j < n - 1 - i; j++){if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}int main(void){int arr[] = { 5, 89, 3, 22, 40, 31, 9, 22, 67, 28, 45, 78 };printf("main: sizeof(arr) = %d\n", sizeof(arr));int n = sizeof(arr) / sizeof(arr[0]);BubbleSort(arr, n);for (size_t i = 0; i < n; i++){printf("%d ", arr[i]);}printf("\n");system("pause");return EXIT_SUCCESS;}

7.3 指针做函数返回值

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int m = 100; // 全局变量 对应空间消失 ==> 程序结束。int* test_func2(int a, int b){int p = 1234;// 局部变量//return &m;return &p;}int main(void){int* ret = NULL;// NULL == 0ret = test_func2(10, 20);printf("ret = %d\n", *ret);system("pause");return EXIT_SUCCESS;}

7.4 指针和字符串

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str1[] = "hello";// {'h',, 'e', 'l', 'l', 'o', '\0'}char m[] = "hello";char* str2 = "hello";// "hello" 是一个字符串常量, 不能修改。char* n = "hello";str1[0] = 'R';str2[0] = 'R';printf("str1 = %p\n", str1);printf("m = %p\n", m);printf("str2 = %p\n", str2);printf("n = %p\n", n);system("pause");return EXIT_SUCCESS;}

7.5 字符串比较

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // str1 和 str2== -->0str1 > str2 --> 1, str1 -1int mystrcmp(char *str1, char *str2){int i = 0;while (str1[i] == str2[i]) // *(str1+i) == *(str2+i){if (str1[i] == '\0'){return 0;// 2字符串一样。}i++;}return str1[i] > str2[i] " />1 : -1;}int mystrcmp2(char *str1, char *str2){while (*str1 == *str2) // *(str1+i) == *(str2+i){if (*str1 == '\0'){return 0;// 2字符串一样。}str1++;str2++;}return *str1 > *str2 ? 1 : -1;}int main(void){char *str1 = "helloz";char *str2 = "helloz";//int ret = mystrcmp(str1, str2);int ret = mystrcmp2(str1, str2);if (ret == 0)printf("相同\n");else if (ret == 1)printf("str1 > str2\n");else if (ret == -1)printf("str1 < str2\n");elseprintf("异常\n");system("pause");return EXIT_SUCCESS;}

7.6 字符串拷贝

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // src: 源dst: 目标//数组版本void mystrcpy(char* src, char* dst){int i = 0;while (src[i] != 0)// src[i] == *(src+i){dst[i] = src[i];i++;}dst[i] = '\0';}//指针版void mystrcpy2(char* src, char* dst){while (*src != '\0')// src[i] == *(src+i){*dst = *src;src++;dst++;}*dst = '\0';}int main(void){char* src = "helloworldfuoie11ll";char dst[100];mystrcpy2(src, dst);printf("dst = %s\n", dst);system("pause");return EXIT_SUCCESS;}

7.7 字符串中查找字符

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // hellowrld --- 'm'char* myStrch(char* str, char ch){while (*str){if (*str == ch){return str;}str++;}return NULL;}// hellowrld --- 'o'char* myStrch2(char* str, char ch){int i = 0;while (str[i]){if (str[i] == ch){return &str[i];}i++;}return NULL;}int main(void){char str[] = "hello world";char ch = ' ';char* ret = NULL;ret = myStrch2(str, ch);printf("ret = %s\n", ret);system("pause");return EXIT_SUCCESS;}

7.8 字符串去除空格

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void str_no_space(char* src, char* dst){int i = 0;int j = 0; while (src[i] != 0){if (src[i] != ' '){dst[j] = src[i];j++;}i++;}dst[j] = '\0';}void str_no_space2(char* src, char* dst){while (*src != 0){if (*src != ' '){*dst = *src;dst++;}src++;}*dst = '\0';}int main(void){char str[] = "ni chou sha chou ni za di";char dst[100] = { 0 };str_no_space2(str, dst);printf("dst = %s\n", dst);system("pause");return EXIT_SUCCESS;}

7.9 字符串中找子串

#include #include #include //strstr函数测试int main(void){char* ret = strstr("hellollollo", "llo");printf("ret = %s\n", ret);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int str_times(char* str, char* substr){int count = 0;char* p = strstr(str, substr);// "llollollo"while (p != NULL){count++;p += strlen(substr);// p = p+strlen(substr) --> "llollo"p = strstr(p, substr);// 返回: "llo"}return count;}// 统计字符串中,子串出现的次数。int main(void){char str[] = "helloabclloxyzllo";char substr[] = "llo";int ret = str_times(str, substr);printf("出现%d次\n", ret);system("pause");return EXIT_SUCCESS;}

8. 字符串和内存

8.1 字符串操作

8.1.1 非空字符串元素个数

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int no_space_str(char* str){int count = 0;char* p = str;while (*p){if (*p != ' '){count++;}p++;}return count;}int main(void){char str[] = "ni chou sha";int ret = no_space_str(str);printf("%d\n", ret);system("pause");return EXIT_SUCCESS;}

8.1.2 字符串逆序

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // o e l l h// // 字符串逆序void str_inserse(char* str){//int i, j;// str[i] *(str+i)char* start = str;// 记录首元素地址char* end = str + strlen(str) - 1;// 记录最后一个元素地址。while (start < end)// 首元素地址是否 < 最后一个元素地址{char tmp = *start;// 三杯水 char 元素交换*start = *end;*end = tmp;start++;// 首元素对应指针后移end--;// 尾元素对应指针前移}}// 判断回文字符串 abcddpbaint str_abcbb(char* str){char* start = str;// 记录首元素地址char* end = str + strlen(str) - 1;// 记录最后一个元素地址。while (start < end)// 首元素地址是否 < 最后一个元素地址{if (*start != *end)// 判断字符是否一致。{return 0; // 0 表示非 回文}start++;end--;}return 1;// 1 表示 回文}int main(void){char str[] = "this is a test";str_inserse(str);printf("str=%s\n ---------------------\n", str);char s2[] = "abcmncba";int ret = str_abcbb(s2);if (ret == 0)printf("不是回文\n");else if (ret == 1)printf("是回文\n");system("pause");return EXIT_SUCCESS;}

8.1.3 字符串拷贝

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // strcpyint main(void){char src[] = "abc efgzhansan wangwu ";char dest[10] = { 0 };char* p = strcpy(dest, src); printf("p= %s\n", p);printf("dest = %s\n", dest);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // strcpy// strncpyint main(void){char src[] = "hello world";char dest[100] = { 0 };char* p = strncpy(dest, src, 100); for (size_t i = 0; i < 10; i++){printf("%c\n", p[i]);}printf("p= %s\n", p);printf("dest = %s\n", dest);system("pause");return EXIT_SUCCESS;}

8.1.4 字符串拼接strcat和strncat

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char src[] = "world";char dest[] = "hello";char* p = strcat(dest, src);printf("p = %s\n", p);printf("dest = %s\n", dest);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char src[] = "world";char dest[6] = "hello";char* p = strncat(dest, src, 3);printf("p = %s\n", p);printf("dest = %s\n", dest);printf("%d\n", strlen(dest));system("pause");return EXIT_SUCCESS;}

8.1.5 strcmp和strcmpz

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // strncpyint main(void){char* str1 = "helloworld";char* str2 = "helloz";printf("ret = %d\n", strcmp(str1, str2));system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // strncpyint main(void){char* str1 = "helloworld";char* str2 = "helloz";printf("ret = %d\n", strncmp(str1, str2, 8));system("pause");return EXIT_SUCCESS;}

8.1.6 格式化度读入和写出sprintf,sscanf

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // sprintfint main(void){char buf[100] = { 0 }; //bufferstring strsource srcsprintf(buf, "%d%c%d=%d\n", 10, '+', 34, 10 + 34);puts(buf);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include // sscanfint main(void){char buf[100] = { 0 }; //bufferstring strsource srcint a, b, c;char str[] = "13+56=89";sscanf(str, "%d+%d=%d", &a, &b, &c);printf("a = %d\n", a);printf("b = %d\n", b);printf("c = %d\n", c);system("pause");return EXIT_SUCCESS;}

8.1.7 字符串分割strtok

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[] = "www.itcast.cn.com.net";// www baidu comchar* p = strtok(str, ".");// 第一次拆分,参1 传 待拆分的原串。while (p != NULL){p = strtok(NULL, ".");// 第1+ 次拆分是,参1传 NULL.printf("%s\n", p);}system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char str[] = "www.baidu.com$This is a strtok$test";char* p = strtok(str, "$ .");//strtok函数的第一个参数不为NULL,函数将找到str中第一个被分割的字符串,同时strtok还会记住该分割符的位置// strtok函数的第一个参数为NULL,函数将在之前strtok记住的分隔符的位置开始,查找下一个标记while (p != NULL){p = strtok(NULL, ". $");printf("p = %s\n", p);}system("pause");return EXIT_SUCCESS;}

8.1.8 atol_atof_atol

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include static int a = 1034673;void test1(void){static int b = 0;printf("b = %d\n", b++);}int main(void){char str[] = "abc345";int num = atoi(str);printf("num = %d\n", num);char str1[] = " -10";int num1 = atoi(str1);printf("num1 = %d\n", num1);char str2[] = "0.123f";double num2 = atof(str2);printf("num2 = %.2lf\n", num2);char str3[] = "123L";long num3 = atol(str3);printf("num3 = %ld\n", num3);system("pause");return EXIT_SUCCESS;}

8.2 局部变量

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void test1(void);// 全局函数声明int m = 4456;int main(void){int i = 10903;for (size_t j = 0; j < 10; j++){printf("j = %d\n", j);//test1();}printf("i 2 = %d\n", i);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){//int arr[1000000] = {10, 20, 40};int *p = (int *)malloc(sizeof(int) * 10);//char *str = (char *)malloc(sizeof(char)*10);if (p == NULL){printf("malloc error\n");return -1;}char *tmp = p;// 记录malloc返回的地址值。用于free// 写数据到 malloc 空间。for (size_t i = 0; i < 10; i++){p[i] = i + 10;}// 读出malloc空间中的数据//for (size_t i = 0; i < 10; i++)//{//printf("%d ", *(p+i));//}for (size_t i = 0; i < 10; i++){printf("%d ", *p);p++;}// 释放申请的内存。free(tmp);p = NULL;system("pause");return EXIT_SUCCESS;}

8.3 申请堆空间

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){//int arr[1000000] = {10, 20, 40};int* p = (int*)malloc(sizeof(int) * 10);//char *str = (char *)malloc(sizeof(char)*10);if (p == NULL){printf("malloc error\n");return -1;}char* tmp = p;// 记录malloc返回的地址值。用于free// 写数据到 malloc 空间。for (size_t i = 0; i < 10; i++){p[i] = i + 10;}// 读出malloc空间中的数据//for (size_t i = 0; i < 10; i++)//{//printf("%d ", *(p+i));//}for (size_t i = 0; i < 10; i++){printf("%d ", *p);p++;}// 释放申请的内存。free(tmp);p = NULL;system("pause");return EXIT_SUCCESS;}

8.4 二级指针malloc空间

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){int** p = malloc(sizeof(int*) * 3);// int **p ==> int *p[10]; ==> [ int *, int *, int * ]for (size_t i = 0; i < 3; i++){p[i] = malloc(sizeof(int) * 5);}// 使用空间 -- 写for (size_t i = 0; i < 3; i++){for (size_t j = 0; j < 5; j++){p[i][j] = i + j;}}// 使用空间 -- 读for (size_t i = 0; i < 3; i++){for (size_t j = 0; j < 5; j++){printf("%d ", *(*(p + i) + j));// p[i][j] == *(p+i)[j] == *(*(p+i)+j)}printf("\n");}// 释放空间时,应先释放内层空间。for (size_t i = 0; i < 3; i++){free(p[i]); //*(p+i)p[i] = NULL;}// 释放外层空间free(p);p = NULL;system("pause");return EXIT_SUCCESS;}

9. 文件

9.1 联合体和枚举

9.1.1 联合体

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include typedef union test {char ch;short sh;int a;}test_t;int main(void){test_t obj;obj.a = 0x87654321;printf("&obj= %p\n", &obj);printf("&obj.ch = %p\n", &obj.ch);printf("&obj.sh = %p\n", &obj.sh);printf("&obj.a= %p\n", &obj.a);printf("sizeof(test_t) = %u\n", sizeof(test_t));printf("a= 0x%x\n", obj.a);printf("sh = 0x%x\n", obj.sh);printf("ch = 0x%x\n", obj.ch);obj.ch = 0xFF;printf("a= 0x%x\n", obj.a);printf("sh = 0x%x\n", obj.sh);printf("ch = 0x%x\n", obj.ch);system("pause");return EXIT_SUCCESS;}enumcolor { red, green = -5, blue, black, pink = 18, yellow };

9.1.2 枚举

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include typedef union test {char ch;short sh;int a;}test_t;enumcolor { red, green = -5, blue, black, pink = 18, yellow };int main(void){int flg = 2;// ......if (flg == blue){printf("blue is 2\n");}else{printf("blue is not 2, blue = %d\n", blue);}printf("yellow = %d\n", yellow);system("pause");return EXIT_SUCCESS;}

9.2 系统文件

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){fclose(stdout);printf("hello file\n");system("pause");return EXIT_SUCCESS;}

9.3 文件的打开和关闭

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){FILE* fp = NULL;fp = fopen("test2.txt", "w");if (fp == NULL){perror("fopen error");//printf("fopen error\n");:xxxxxxxgetchar();return -1;}fclose(fp);printf("------------finish\n");system("pause");return EXIT_SUCCESS;}

9.4 fput和fget

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char *filename = "test04.txt";FILE *fp = fopen(filename, "w");if (fp == NULL){perror("fopen error");return -1;}int ret = fputc('A', fp);printf("ret = %d\n", ret);fclose(fp);printf("---------------finish\n");system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){char* buf = "abcdefghijklmnopqrstuvwxyz";char* filename = "test04.txt";int ret = 0;FILE* fp = fopen(filename, "w");if (fp == NULL){perror("fopen error");return -1;}int n = strlen(buf);for (size_t i = 0; i < n; i++){ret = fputc(buf[i], fp);if (ret == -1){perror("fputc eror");return -1;}}fclose(fp);system("pause");return EXIT_SUCCESS;}

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void write_file(){FILE* fp = fopen("05test.txt", "w");if (fp == NULL){perror("fopen error");return;}fputc('a', fp);fputc('b', fp);fputc('c', fp);fputc('d', fp);fclose(fp);}void read_file(){char ch = 0;FILE* fp = fopen("05test.txt", "r");if (fp == NULL){perror("fopen error");return;}while (1){ch = fgetc(fp);// 读取一个字符if (ch == EOF){break;}printf("%d\n", ch);}fclose(fp);}int main(void){//write_file();read_file();system("pause");return EXIT_SUCCESS;}

9.5 feof

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void read_file06(){char ch = 0;FILE* fp = fopen("06test.txt", "r");if (fp == NULL){perror("fopen error");return;}while (1){ch = fgetc(fp);if (feof(fp)){break;}printf("%d\n", ch);}fclose(fp);}void test_feof(){FILE* fp = fopen("06test.txt", "r");if (fp == NULL){perror("fopen error");return;}while (1){printf("没有到达文件结尾\n");fgetc(fp);// 一次读一个字符,读到字符直接丢弃。if (feof(fp)){break;}}fclose(fp);}void write_file06(){FILE* fp = fopen("06test.txt", "w");if (fp == NULL){perror("fopen error");return;}fputc('a', fp);fputc('b', fp);fputc(-1, fp);fputc('c', fp);fputc('d', fp);fputc('\n', fp);fclose(fp);}int main(void){write_file06();read_file06();test_feof();system("pause");return EXIT_SUCCESS;}

9.6 fgets和fputs

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){FILE* fp = fopen("test07.txt", "w");if (fp == NULL){perror("fopen error");return -1;}char buf[4096] = { 0 };while (1){fgets(buf, 4096, stdin);if (strcmp(buf, ":wq\n") == 0){break;}fputs(buf, fp);}fclose(fp);system("pause");return EXIT_SUCCESS;}

9.7 四则运算(文件版)

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void write_file08(){FILE* fp = fopen("test08.txt", "w");if (fp == NULL){perror("fopen error");return;}fputs("10/2=\n", fp);fputs("10*3=\n", fp);fputs("4-2=\n", fp);fputs("10+2=\n", fp);fclose(fp);}int calc(char ch, int a, int b){switch (ch){case '/':return a / b;case '+':return a + b;case '-':return a - b;case '*':return a * b;default:break;}}void read_file08(){char buf[4096] = { 0 };char result[4096] = { 0 };char sum_res[4096] = { 0 };int a, b, ret;char ch;FILE* fp = fopen("test08.txt", "r");if (fp == NULL){perror("fopen error");return;}while (1){fgets(buf, 4096, fp);//buf = "10/2=\n\0";if (feof(fp)){break;}sscanf(buf, "%d%c%d=\n", &a, &ch, &b);// a:10, ch:'/' b: 2sprintf(result, "%d%c%d=%d\n", a, ch, b, calc(ch, a, b));// 10 / 2 = 5;strcat(sum_res, result);}fclose(fp);// 将 只有表达式没有结果的文件关闭。fp = fopen("test08.txt", "w");// 清空 只有表达式没有结果的文件if (fp == NULL){perror("fopen error");return;}fputs(sum_res, fp);// 将 既有表达式又有结果的字符串写到文件中。fclose(fp);}int main(void){write_file08();getchar();read_file08();system("pause");return EXIT_SUCCESS;}

9.8 sprintf和sscanf

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void write_file(){FILE* fp = fopen("abc.c", "w");if (!fp)// fp == NULL{perror("fopen error");return -1;}fprintf(fp, "%d\n", 10);fprintf(fp, "%d\n", 8);fprintf(fp, "%d\n", 6);fclose(fp);}void read_file(){int a;FILE* fp = fopen("abc.c", "r");if (!fp)// fp == NULL{perror("fopen error");return -1;}fscanf(fp, "%d\n", &a);printf("%d\n", a);fscanf(fp, "%d\n", &a);printf("%d\n", a);fscanf(fp, "%d\n", &a);printf("%d\n", a);a = 0;fscanf(fp, "%d\n", &a);printf("%d\n", a);fclose(fp);}// fscanf 循环读文件void read_file2(){int a;FILE* fp = fopen("abc.c", "r");if (!fp)// fp == NULL{perror("fopen error");return -1;}while (1){fscanf(fp, "%d\n", &a);// 读printf("%d\n", a);if (feof(fp))// 真-- 文件结尾break;}fclose(fp);}// fgets 循环读文件void read_file3(){char buf[1024];FILE* fp = fopen("abc.c", "r");if (!fp)// fp == NULL{perror("fopen error");return -1;}while (1){memset(buf, 0, 1024);fgets(buf, 1024, fp);// 读 \nif (feof(fp))// 真-- 文件结尾break;printf("%d\n", buf[0]);}fclose(fp);}int main(void){FILE* fp = fopen("test0101.txt", "r");if (!fp)// fp == NULL{perror("fopen error");return -1;}int a;char ch;char str[10];int ret = fscanf(fp, "%d %c %s", &a, &ch, str);printf("ret = %d\n", ret);fclose(fp);system("pause");return EXIT_SUCCESS;}

9.9 文件版随机排序

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void write_rand(){FILE *fp = fopen("test02.txt", "w");if (!fp)// fp == NULL{perror("fopen error");return -1;}srand(time(NULL)); // 随机数种子for (size_t i = 0; i < 10; i++){fprintf(fp, "%d\n", rand() % 100);// 将生成的随机数写入文件}fclose(fp);}void BubbleSort(int * src, int len){for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - 1 - i; j++){if (src[j] > src[j + 1]){int temp = src[j];src[j] = src[j + 1];src[j + 1] = temp;}}}}void read_rand02(){int arr[10], i = 0;FILE *fp = fopen("test02.txt", "r");if (!fp)// fp == NULL{perror("fopen error");return -1;}while (1){fscanf(fp, "%d\n", &arr[i]);// 从文件中读取一个随机数,存入数组arri++;if (feof(fp))// 先存储,后判断,防止最后一个元素丢失break;}BubbleSort(arr, sizeof(arr)/sizeof(arr[0]));// 对读取到的乱序数组排序fclose(fp);// 关闭文件fp = fopen("test02.txt", "w");// 重新w方式打开文件, 清空原未排序文件。if (!fp)// fp == NULL{perror("fopen error");return -1;}for (size_t i = 0; i < 10; i++)fprintf(fp, "%d\n", arr[i]);// 写排好序的数组到文件fclose(fp);}int main(void){write_rand();getchar();read_rand02();system("pause");return EXIT_SUCCESS;}

9.10 结构化读取fwrite和fread

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include typedef struct student {int age;char name[10];int num;} stu_t;void write_struct(){stu_t stu[4] = {18, "afei", 10,20, "andy", 20,30, "lily", 30,16, "james", 40};FILE *fp = fopen("test03.txt", "w");if (!fp){perror("fopen error");return -1;}int ret = fwrite(&stu[0], 1, sizeof(stu_t) * 4, fp);if (ret == 0){perror("fwrite error");return -1;}printf("ret = %d\n", ret);fclose(fp);}// 一次读一个元素。void read_struct(){FILE *fp = fopen("test03.txt", "r");if (!fp){perror("fopen error");return -1;}stu_t s1;int ret = fread(&s1, 1, sizeof(stu_t), fp);printf("ret = %d\n", ret);printf("age = %d, name=%s, num = %d\n", s1.age, s1.name, s1.num);fclose(fp);}// 读所有元素void read_struct2(){FILE *fp = fopen("test03.txt", "r");if (!fp){perror("fopen error");return -1;}stu_t s1[10];// stu_t *s1 = malloc(sizeof(stu_t) * 1024);int i = 0;while (1){int ret = fread(&s1[i], 1, sizeof(stu_t), fp);//if (ret == 0)// 替代feof()函数来判断读到文件结尾。if (feof(fp)){break;}i++;printf("age = %d, name=%s, num = %d\n", s1[i].age, s1[i].name, s1[i].num);}fclose(fp);}int main(void){write_struct();read_struct2();system("pause");return EXIT_SUCCESS;}

9.11 大文件拷贝

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include void myfile_cp(){FILE *rfp = fopen("demo.avi", "rb");FILE *wfp = fopen("mycopy.avi", "wb");char buf[4096] = {0};int ret = 0;while (1){memset(buf, 0, sizeof(buf));ret = fread(buf, 1, sizeof(buf), rfp);if (ret == 0){break;}printf("ret = %d\n", ret);fwrite(buf, 1, ret, wfp);}fclose(wfp);fclose(rfp);}int main(void){myfile_cp();printf("---------------------finish\n");system("pause");return EXIT_SUCCESS;}

9.12 随机读取文件

9.12.1 fseek

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include typedef struct student {int age;char name[10];int num;} stu_t;int main(void){stu_t stu[4] = {18, "afei", 10,20, "andy", 20,30, "lily", 30,16, "james", 40};stu_t s1;FILE* fp = fopen("test05.txt", "wb+");if (!fp)// fp == NULL{perror("fopen error");return -1;}int ret = fwrite(&stu[0], 1, sizeof(stu), fp);// 以二进制形式写入,printf("ret = %d\n", ret);fseek(fp, sizeof(stu_t) * 2, SEEK_SET);// 从文件起始位置,向后偏移一个stu结构体ret = fread(&s1, 1, sizeof(s1), fp);printf("ret = %d\n", ret);printf("1 age= %d, name=%s, num=%d\n", s1.age, s1.name, s1.num);int len = ftell(fp); // 获取文件当前读写指针位置,到文件起始位置的偏移量。printf("len = %d\n", len);rewind(fp);// 将文件读写指针回卷到起始。ret = fread(&s1, 1, sizeof(s1), fp);printf("2 age= %d, name=%s, num=%d\n", s1.age, s1.name, s1.num);// 获取文件大小。fseek(fp, 0, SEEK_END);// 将文件读写指针放到文件结尾。len = ftell(fp);printf("文件大小为:%d\n", len);fclose(fp);system("pause");return EXIT_SUCCESS;}

9.12.2 改变指针位置

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main(void){FILE *fp = fopen("test0501.txt", "w+");// "r+"int ret = fputs("11111", fp);printf("ret 1 = %d\n", ret);// 0 表示成功。ret = fputs("22222", fp);printf("ret 2 = %d\n", ret);ret = fputs("33333", fp);printf("ret 3 = %d\n", ret);char buf[1024] = { 0 };//fseek(fp, 5 * 2, SEEK_SET);// 改变读写指针位置。rewind(fp); // 起始位置。char *ptr = fgets(buf, 1024, fp);if (ptr == NULL)printf("ptr == NULL \n");printf("fgets ptr = %s\n", ptr);printf("buf = %s\n", buf);fclose(fp);system("pause");return EXIT_SUCCESS;}

9.12.3 读取指针位置

#include #include #include int main(int argc, char *argv[]){FILE *fp = fopen("test1.txt", "r+");char buf[6] = { 0 };char *ptr = fgets(buf, 6, fp);printf("buf=%s, ptr=%s\n", ptr, buf);fseek(fp, 0, SEEK_CUR);int ret = fputs("AAAAA", fp);printf("ret = %d\n", ret);fclose(fp);system("pause");return 0;}

9.13 获取文件属性

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #include #include /*FILE *fp = fopen("test05.txt", "r");fseek(fp, 0, SEEK_END);int len = ftell(fp);printf("文件大小:%d\n", len);fclose(fp);*/int main(void){struct stat buf;int ret = stat("test05.txt", &buf);// 传出参数:在函数调用结束时,充当函数返回值。printf("文件大小:%d\n", buf.st_size); // 不打开文件,获取文件大小。system("pause");return EXIT_SUCCESS;}

9.14 缓存区刷新

#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include int main0701(void){FILE *fp = fopen("test07.txt", "w+");if (!fp){perror("fopen error");return -1;}char m = 0;while (1) {scanf("%c", &m);if (m == ':'){break;}fputc(m, fp);fflush(fp);// 手动刷新文件缓冲到物理磁盘。}// 当文件关闭时,会自动刷新缓冲区。fclose(fp);system("pause");return EXIT_SUCCESS;}