当前位置:   article > 正文

深究C语言6.文件操作_file *fp; char usr [30],usr1 [30],pwd [10]; int i,

file *fp; char usr [30],usr1 [30],pwd [10]; int i,check=0; strcpy(usr,psu->u

目录

一,文件的定义

二,文件的一些基本常识

1.文件的分类

2.一个文件要有自己唯一的文件标识,以便用户识别和引用

3.文件名的组成

三,文件的概念

一,

二,着重讲一下二进制文件与文本文件的区别

1.文本文件:以字符ASCLL码值进行存储和编码的文件,其文件内容就是字符。

2.二进制文件:存储二进制数据的文件。

三,文件缓冲系统

 四,文件类型指针

五,文件处理步骤

1.定义文件指针

2.打开文件:文件指针指向磁盘文件缓冲区

3.文件处理:对文件数据的处理

4.关闭文件:

六,文件路径

 七,文件处理权限

最高兴的地方来了,上例题。

1.用户信息加密与校验

2.复制用户信息文件,将一个文件复制一份存到另一个文件中

3.用户合法性校验

4.将用户信息以加密的方式进行输出

5.文件综合应用-资金账户管理



一,文件的定义

何为文件?说白了,就是存在外存上面的一块空间,给他起了一个名字罢了。

***那么我们为什么要去使用文件?

许多程序在实现过程中,依赖于把数据存到变量中,而变量是通过内存单元储存数据,数据的处理完全由程序控制,当一个程序运行完成或者终止运行后,所有变量的值都不再保存。

另外,一般的程序都会有输入输出,如果输出输入量不大,那还好,可以慢慢的输入,若数据量很大呢?还去一个一个的输?

文件是解决上述问题的有效方法,它通过把数据存储到磁盘文件中,得以长期保存。实现了数据的持久化使用。

当有大量数据输入时,可通过编译工具实先建立输入数据的文件,程序运行时将不在从键盘输入,直接从文件中读取即可。从而实现数据的一次输入多次使用。同时,当有大量的数据输出时,可以将其输出到指定的文件中,使其的输出不受屏幕大小的限制,并且任何时候都可以查看文件。

二,文件的一些基本常识

1.文件的分类

文件分为程序文件和数据文件,

程序文件又分源文件,目标文件(obj),可执行文件(exe)等等

数据文件就分为常见的二进制文件和文本文件了()内容不一定是程序,而是程序运行时写的数据

2.一个文件要有自己唯一的文件标识,以便用户识别和引用

3.文件名的组成

文件路径+文件名主干+文件后缀

例如c:\code\test.txt

c:\code是文件路径

test.txt是文件名主干

.txt是文件后缀

三,文件的概念

一,

1.文件系统功能是操作系统的重要功能和组成部分。

2.文件可以通过应用程序创建

3.在操作系统中,文件是指驻留在外部介质(如磁盘)中的一个有序数据集。

4.若在记事本编辑文件时不保存,那么数据也就不会保存下来,一般来说,数据是先存到内存中,待程序结束之后再存到硬盘中。

5.文件是一种数据组织方式,是C语言程序处理的对象。

二,着重讲一下二进制文件与文本文件的区别

在C语言中,按数据存储的编码形式,数据文件可分为文本文件和二进制文件

1.文本文件:以字符ASCLL码值进行存储和编码的文件,其文件内容就是字符。

2.二进制文件:存储二进制数据的文件。

从文件的逻辑结构上来看,C语言把文件看作数据流,并将数据按顺序以一维方式存储。

文件的数据流又分为字符流(文本文件)和二进制流(二进制文件)。

**字符串以二进制流或者字符流方式存储的存储内容一致。

三,文件缓冲系统

 

 文件系统分为缓冲文件系统与非缓冲文件系统

****对于缓冲文件系统,在进行文件操作是,系统自动为每一个文件分配一块文件内存缓冲区(内存单元),c程序对文件的所有操作就通过对文件缓冲区的操作来完成。

***对于非缓冲文件系统,文件缓冲区不是系统自动分配,而需要编程者在程序中用c语句实现分配。

 四,文件类型指针

由上可知,文件缓冲区是内存中用于数据存储的数据块,在文件处理过程中,程序需要访问该文件缓冲区实现数据的使用。

但是,文件缓冲区的分配是不能确定的,那我们应该如何找到这个缓冲区呢?

于是乎,C语言引进FILE文件结构,其成员指针指向文件的缓冲区,通过移动指针实现对文件的操作。

***C语言中的文件操作都是通过调用标准函数来实现的,由于结构指针的参数传递效率更高,因此C语言文件操作统一以文件指针方式实现。

***文件指针不像以前普通指针那样能进行fp++或*fp等操作,fp++将意味着指向下一个FILE结构。(如果存在)。

文件打开的实质是把磁盘文件与文件缓冲区对应起来,这样后面的文件操作只需要使用文件指针即可。

五,文件处理步骤

1.定义文件指针

2.打开文件:文件指针指向磁盘文件缓冲区

打开文件功能用于建立系统与要操作的某个文件之间的关联,指定这个文件名并且请求系统分配相应的文件缓冲区内存单元。

3.文件处理:对文件数据的处理

4.关闭文件:

当文件操作完成后,应及时关闭它以防止不正常的操作。关闭文件操作处理强制把缓冲区中的数据写入磁盘外,还将释放文件缓冲区单元和FILE结构,使文件指针与具体文件脱钩

六,文件路径

路径分为绝对路径和相对路径

如不写出路径,则默认与应用程序的当前路径相同,文件路径若包含绝对路径则用//表示。

 七,文件处理权限

 

最高兴的地方来了,上例题。

1.用户信息加密与校验

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //用户信息加密与校验
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. //定义系统用户账号信息结构
  7. struct sysuser {
  8. char username[20];
  9. char password[8];
  10. };
  11. void encrypt(char* pwd);
  12. int main() {
  13. FILE* fp;
  14. int i;
  15. struct sysuser su;
  16. //打开文件并判断
  17. if ((fp = fopen("f12-2.txt", "w")) == NULL) {
  18. printf("File open error\n");
  19. exit(1);
  20. }
  21. //讲n位用户的信息写入文件
  22. for (i = 1; i <= 5; i++) {
  23. printf("Enter %d th sysuser(name password):", i);
  24. //先输入数据,再写入文件中
  25. scanf("%s%s", su.username, su.password);
  26. //加密函数
  27. encrypt(su.password);
  28. //写文件
  29. fprintf(fp, "%s %s \n", su.username, su.password);
  30. }
  31. //关闭文件并判断
  32. if (fclose(fp)) {
  33. printf("Can not close the file !");
  34. exit(1);
  35. }
  36. return 0;
  37. }
  38. //加密函数
  39. void encrypt(char* pwd) {
  40. int i;
  41. //15异或。实现低四位取反,高四位保持不变
  42. for (i = 0; i < strlen(pwd); i++)
  43. pwd[i] = pwd[i] ^ 15;
  44. }

2.复制用户信息文件,将一个文件复制一份存到另一个文件中

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //复制用户信息文件,将一个文件复制一份存到另一个文件中
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. int main() {
  6. FILE* fp1, * fp2;
  7. char ch;
  8. if ((fp1 = fopen("f12-2.txt", "r")) == NULL) {
  9. printf("File open error!");
  10. exit(0);
  11. }
  12. if ((fp2 = fopen("f12-3.txt", "w")) == NULL) {
  13. printf("File open error!");
  14. exit(0);
  15. }
  16. while (!feof(fp1)) {
  17. ch = fgetc(fp1);
  18. if (ch != EOF)
  19. fputc(ch, fp2);
  20. }
  21. if (fclose(fp1)) {
  22. printf("Can not close the file !\n");
  23. exit(0);
  24. }
  25. if (fclose(fp2)) {
  26. printf("Can not close the file !\n");
  27. exit(0);
  28. }
  29. return 0;
  30. }

3.用户合法性校验

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //用户合法性校验
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. //定义系统用户账号信息结构
  7. struct sysuser {
  8. char username[20];
  9. char password[8];
  10. };
  11. //加密算法
  12. void encrypt(char* pwd);
  13. //校验用户信息合法性
  14. int checkUserValid(struct sysuser* psu);
  15. int main() {
  16. struct sysuser su;
  17. //输入待校验的用户名
  18. printf("Enter username:");
  19. scanf("%s", su.username);
  20. //输入待校验的密码
  21. printf("Enter password:");
  22. scanf("%s", su.password);
  23. //调用函数判断
  24. if (checkUserValid(&su) == 1)
  25. printf("Valid user!\n");
  26. else
  27. printf("Invalid user!\n");
  28. return 0;
  29. }
  30. void encrypt(char* pwd) {
  31. int i = 0;
  32. for (i = 0; i < strlen(pwd); i++)
  33. pwd[i] = pwd[i] ^ 15;
  34. }
  35. //校验用户信息的合法性,成功则返回1,失败返回0
  36. int checkUserValid(struct sysuser* psu) {
  37. FILE* fp;
  38. char usr[30], usr1[30], pwd[10];
  39. int check = 0;
  40. //为了不改变用户本来的信息,另外开辟空间。这些数据都是待校验的信息
  41. strcpy(usr, psu->username);
  42. strcpy(pwd, psu->password);
  43. //将待校验的密码进行加密,然后进行比对
  44. encrypt(pwd);
  45. //将待校验的信息写成与正确信息写成同一种形式。然后方便更好的校验
  46. //另一个知识点,strcat的后面一个表达式是一个const类型的char*指针,所以要用“”而不用‘’。
  47. strcat(usr, " ");
  48. strcat(usr, pwd);
  49. strcat(usr, "\n");
  50. //打开文件
  51. if ((fp = fopen("f12-2.txt", "r")) == NULL) {
  52. printf("File open error!\n");
  53. exit(0);
  54. }
  55. //遍历文件,然后运用strcmp进行比较,比较两个字符串是否相等
  56. while (!feof(fp)) {
  57. fgets(usr1, 30, fp);
  58. if (strcmp(usr, usr1) == 0) {
  59. check = 1;
  60. break;
  61. }
  62. }
  63. //关闭文件
  64. if (fclose(fp)) {
  65. printf("Can not close the file!\n");
  66. exit(0);
  67. }
  68. return check;
  69. }

4.将用户信息以加密的方式进行输出

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. //定义系统用户账号信息结构
  6. struct sysuser {
  7. char username[20];
  8. char password[8];
  9. };
  10. #define SIZE 5
  11. //加密算法
  12. void encrypt(char* pwd);
  13. int main() {
  14. FILE* fp;
  15. int i;
  16. struct sysuser u[SIZE], su[SIZE], * pu = u, * psu = su;
  17. //打开文件
  18. if ((fp = fopen("f12-5.dat", "wb+")) == NULL) {
  19. printf("File open error!\n");
  20. exit(0);
  21. }
  22. //输入SIZE个用户信息,并对密码加密,保存在结构数组u中
  23. for (i = 1; i <= SIZE; i++) {
  24. printf("Enter %d th sysuser (name password):", i);
  25. scanf("%s%s", pu->username, pu->password);
  26. encrypt(pu->password);
  27. }
  28. //u是固定的,将pu返回到u地址处。
  29. pu = u;
  30. fwrite(pu, sizeof(struct sysuser), SIZE, fp);
  31. rewind(fp);
  32. fread(psu, sizeof(struct sysuser), SIZE, fp);
  33. //将数据输出到屏幕上
  34. for (i = 0; i < SIZE; i++, psu++) {
  35. printf("%-10s%s\n", psu->username, psu->password);
  36. }
  37. //关闭文件
  38. if (fclose(fp)) {
  39. printf("File can not close!");
  40. exit(0);
  41. }
  42. return 0;
  43. }
  44. //加密算法
  45. void encrypt(char* pwd) {
  46. for (int i = 0; i < strlen(pwd); i++) {
  47. pwd[i] = pwd[i] ^ 15;
  48. }
  49. }

5.文件综合应用-资金账户管理

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. //文件综合应用-资金账户管理
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include"process.h"
  6. //用来保存sizeof(struct LogData)
  7. long size;
  8. //定义结构用来存放用户需要的数据
  9. struct LogData {
  10. //记录id
  11. long logid;
  12. //记录发生日期
  13. char logdate[11];
  14. //记录事件说明
  15. char lognote[15];
  16. //发生费用:正代表收入,负代表支出
  17. double charge;
  18. //余额
  19. double balance;
  20. };
  21. //菜单(选择想进行的操作)
  22. int inputchoice() {
  23. int mychoice;
  24. printf("\nEnter your choice:\n");
  25. printf("1-Add a new cash LOG.\n2-List All Cash LOG.\n");
  26. printf("3-Query Last Cash LOG.\n0-End program.\n");
  27. scanf("%d", &mychoice);
  28. return mychoice;
  29. }
  30. //获取文件记录总数
  31. long getLogcount(FILE* cfptr) {
  32. long begin, end, logcount;
  33. fseek(cfptr, 0L, SEEK_SET);
  34. begin = ftell(cfptr);
  35. fseek(cfptr, size, SEEK_END);
  36. end = ftell(cfptr);
  37. //每一次操作都会存上一个结构体
  38. logcount = (end - begin) / size;
  39. return logcount;
  40. }
  41. //列出所有收支流水记录
  42. void ListAllLog(FILE* cfptr) {
  43. struct LogData log;
  44. //定位指针到文件开始位置
  45. fseek(cfptr, 0L, SEEK_SET);
  46. //读文件,将1size大小的数据从文件中读出,传到log中
  47. fread(&log, size, 1, cfptr);
  48. printf("logid logdate lognote charge balance\n");
  49. while (!feof(cfptr)) {
  50. //输出数据
  51. printf("%61d&-11s%-15s%10.2lf%10.2lf",
  52. log.logid, log.logdate, log.lognote, log.charge, log.balance);
  53. //再次开始读取
  54. //为什么要一个一个的读取,不选择直接一次性读完,因为要换行,增加可读性
  55. fread(&log,size, 1, cfptr);
  56. }
  57. }
  58. //查询显示最后一条记录
  59. void QueryLastLog(FILE* cfptr) {
  60. struct LogData log;
  61. long logcount;
  62. logcount = getLogcount(cfptr);
  63. //表示有过操作,有记录存在
  64. if (logcount > 0) {
  65. //读取最后一次记录
  66. fseek(cfptr, size * (logcount - 1), SEEK_SET);
  67. fread(&log, size, 1, cfptr);
  68. printf("The Last log is:\n");
  69. printf("logid:%-61d\nlogdate:%-11s\nlognote:%-15s\n",
  70. log.logid, log.logdate, log.lognote);
  71. printf("charge:%-10.2lf\nbalance:%-10.2lf\n",
  72. log.charge, log.balance);
  73. }
  74. //如果没有过任何操作
  75. else
  76. printf("no logs in file!\n");
  77. }
  78. //添加新纪录
  79. void AddNewLog(FILE* cfptr) {
  80. struct LogData log, lastlog;
  81. long logcount;
  82. //初始化这次操作的数据
  83. printf("Input logdate(format:2006-01-01):");
  84. scanf("%s", log.logdate);
  85. printf("Input lognote:");
  86. scanf("%s", log.lognote);
  87. printf("Input Charge:Income+and expend-:");
  88. scanf("%lf", &log.charge);
  89. //获取记录数
  90. logcount = getLogcount(cfptr);
  91. //若之前有过操作,
  92. if (logcount > 0) {
  93. fseek(cfptr, size * (logcount - 1), SEEK_SET);
  94. //读出文件,并且修改数据
  95. fread(&lastlog, size, 1, cfptr);
  96. log.balance = lastlog.logid + 1;
  97. log.balance = log.charge + lastlog.balance;
  98. }
  99. //如果是第一次操作
  100. else {
  101. log.logid = 1;
  102. log.balance = log.charge;
  103. }
  104. //操作完之后,一定要将文件指针指向文件开头才能写文件
  105. rewind(cfptr);
  106. printf("logid=%ld\n", log.logid);
  107. fwrite(&log, sizeof(struct LogData), 1, cfptr);
  108. }
  109. enum Choice {
  110. addNewLog=1,
  111. listAllLog,
  112. queryLastLog
  113. };
  114. int main() {
  115. FILE* fp;
  116. int choice;
  117. //if ((fp = fopen("cashbox.dat", openmode)) == NULL)
  118. if ((fp = fopen("cashbox.dat", "r")) == NULL) {
  119. printf("can not open file cashbox.dat!\n");
  120. exit(0);
  121. }
  122. size = sizeof(struct LogData);
  123. while ((choice = inputchoice()) != 0){
  124. switch (choice) {
  125. case addNewLog: AddNewLog(fp);
  126. break;
  127. case listAllLog:ListAllLog(fp);
  128. break;
  129. case queryLastLog:QueryLastLog(fp);
  130. break;
  131. default:printf("Input Error.");
  132. break;
  133. }
  134. }
  135. if (fclose(fp)) {
  136. printf("can not close the file!\n");
  137. exit(0);
  138. }
  139. return 0;
  140. }

后面我会专门再写一章文件操作的标准函数篇,在文件操作中,这些标准函数的使用一定是要十分熟悉的。

同志们继续努力,革命还未成功呢!加油哦。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/208455
推荐阅读
相关标签
  

闽ICP备14008679号