切近学习者的C语言讲解合集_哔哩哔哩_bilibili 上述是我在B站发布的相应视频
目录
1.文本文件:以字符ASCLL码值进行存储和编码的文件,其文件内容就是字符。
一,文件的定义
何为文件?说白了,就是存在外存上面的一块空间,给他起了一个名字罢了。
***那么我们为什么要去使用文件?
许多程序在实现过程中,依赖于把数据存到变量中,而变量是通过内存单元储存数据,数据的处理完全由程序控制,当一个程序运行完成或者终止运行后,所有变量的值都不再保存。
另外,一般的程序都会有输入输出,如果输出输入量不大,那还好,可以慢慢的输入,若数据量很大呢?还去一个一个的输?
文件是解决上述问题的有效方法,它通过把数据存储到磁盘文件中,得以长期保存。实现了数据的持久化使用。
当有大量数据输入时,可通过编译工具实先建立输入数据的文件,程序运行时将不在从键盘输入,直接从文件中读取即可。从而实现数据的一次输入多次使用。同时,当有大量的数据输出时,可以将其输出到指定的文件中,使其的输出不受屏幕大小的限制,并且任何时候都可以查看文件。
二,文件的一些基本常识
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.用户信息加密与校验
#define _CRT_SECURE_NO_WARNINGS 1
//用户信息加密与校验
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//定义系统用户账号信息结构
struct sysuser {
char username[20];
char password[8];
};
void encrypt(char* pwd);
int main() {
FILE* fp;
int i;
struct sysuser su;
//打开文件并判断
if ((fp = fopen("f12-2.txt", "w")) == NULL) {
printf("File open error\n");
exit(1);
}
//讲n位用户的信息写入文件
for (i = 1; i <= 5; i++) {
printf("Enter %d th sysuser(name password):", i);
//先输入数据,再写入文件中
scanf("%s%s", su.username, su.password);
//加密函数
encrypt(su.password);
//写文件
fprintf(fp, "%s %s \n", su.username, su.password);
}
//关闭文件并判断
if (fclose(fp)) {
printf("Can not close the file !");
exit(1);
}
return 0;
}
//加密函数
void encrypt(char* pwd) {
int i;
//与15异或。实现低四位取反,高四位保持不变
for (i = 0; i < strlen(pwd); i++)
pwd[i] = pwd[i] ^ 15;
}
2.复制用户信息文件,将一个文件复制一份存到另一个文件中
#define _CRT_SECURE_NO_WARNINGS 1
//复制用户信息文件,将一个文件复制一份存到另一个文件中
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE* fp1, * fp2;
char ch;
if ((fp1 = fopen("f12-2.txt", "r")) == NULL) {
printf("File open error!");
exit(0);
}
if ((fp2 = fopen("f12-3.txt", "w")) == NULL) {
printf("File open error!");
exit(0);
}
while (!feof(fp1)) {
ch = fgetc(fp1);
if (ch != EOF)
fputc(ch, fp2);
}
if (fclose(fp1)) {
printf("Can not close the file !\n");
exit(0);
}
if (fclose(fp2)) {
printf("Can not close the file !\n");
exit(0);
}
return 0;
}
3.用户合法性校验
#define _CRT_SECURE_NO_WARNINGS 1
//用户合法性校验
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//定义系统用户账号信息结构
struct sysuser {
char username[20];
char password[8];
};
//加密算法
void encrypt(char* pwd);
//校验用户信息合法性
int checkUserValid(struct sysuser* psu);
int main() {
struct sysuser su;
//输入待校验的用户名
printf("Enter username:");
scanf("%s", su.username);
//输入待校验的密码
printf("Enter password:");
scanf("%s", su.password);
//调用函数判断
if (checkUserValid(&su) == 1)
printf("Valid user!\n");
else
printf("Invalid user!\n");
return 0;
}
void encrypt(char* pwd) {
int i = 0;
for (i = 0; i < strlen(pwd); i++)
pwd[i] = pwd[i] ^ 15;
}
//校验用户信息的合法性,成功则返回1,失败返回0;
int checkUserValid(struct sysuser* psu) {
FILE* fp;
char usr[30], usr1[30], pwd[10];
int check = 0;
//为了不改变用户本来的信息,另外开辟空间。这些数据都是待校验的信息
strcpy(usr, psu->username);
strcpy(pwd, psu->password);
//将待校验的密码进行加密,然后进行比对
encrypt(pwd);
//将待校验的信息写成与正确信息写成同一种形式。然后方便更好的校验
//另一个知识点,strcat的后面一个表达式是一个const类型的char*指针,所以要用“”而不用‘’。
strcat(usr, " ");
strcat(usr, pwd);
strcat(usr, "\n");
//打开文件
if ((fp = fopen("f12-2.txt", "r")) == NULL) {
printf("File open error!\n");
exit(0);
}
//遍历文件,然后运用strcmp进行比较,比较两个字符串是否相等
while (!feof(fp)) {
fgets(usr1, 30, fp);
if (strcmp(usr, usr1) == 0) {
check = 1;
break;
}
}
//关闭文件
if (fclose(fp)) {
printf("Can not close the file!\n");
exit(0);
}
return check;
}
4.将用户信息以加密的方式进行输出
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//定义系统用户账号信息结构
struct sysuser {
char username[20];
char password[8];
};
#define SIZE 5
//加密算法
void encrypt(char* pwd);
int main() {
FILE* fp;
int i;
struct sysuser u[SIZE], su[SIZE], * pu = u, * psu = su;
//打开文件
if ((fp = fopen("f12-5.dat", "wb+")) == NULL) {
printf("File open error!\n");
exit(0);
}
//输入SIZE个用户信息,并对密码加密,保存在结构数组u中
for (i = 1; i <= SIZE; i++) {
printf("Enter %d th sysuser (name password):", i);
scanf("%s%s", pu->username, pu->password);
encrypt(pu->password);
}
//u是固定的,将pu返回到u地址处。
pu = u;
fwrite(pu, sizeof(struct sysuser), SIZE, fp);
rewind(fp);
fread(psu, sizeof(struct sysuser), SIZE, fp);
//将数据输出到屏幕上
for (i = 0; i < SIZE; i++, psu++) {
printf("%-10s%s\n", psu->username, psu->password);
}
//关闭文件
if (fclose(fp)) {
printf("File can not close!");
exit(0);
}
return 0;
}
//加密算法
void encrypt(char* pwd) {
for (int i = 0; i < strlen(pwd); i++) {
pwd[i] = pwd[i] ^ 15;
}
}
5.文件综合应用-资金账户管理
#define _CRT_SECURE_NO_WARNINGS 1
//文件综合应用-资金账户管理
#include<stdio.h>
#include<stdlib.h>
#include"process.h"
//用来保存sizeof(struct LogData)
long size;
//定义结构用来存放用户需要的数据
struct LogData {
//记录id
long logid;
//记录发生日期
char logdate[11];
//记录事件说明
char lognote[15];
//发生费用:正代表收入,负代表支出
double charge;
//余额
double balance;
};
//菜单(选择想进行的操作)
int inputchoice() {
int mychoice;
printf("\nEnter your choice:\n");
printf("1-Add a new cash LOG.\n2-List All Cash LOG.\n");
printf("3-Query Last Cash LOG.\n0-End program.\n");
scanf("%d", &mychoice);
return mychoice;
}
//获取文件记录总数
long getLogcount(FILE* cfptr) {
long begin, end, logcount;
fseek(cfptr, 0L, SEEK_SET);
begin = ftell(cfptr);
fseek(cfptr, size, SEEK_END);
end = ftell(cfptr);
//每一次操作都会存上一个结构体
logcount = (end - begin) / size;
return logcount;
}
//列出所有收支流水记录
void ListAllLog(FILE* cfptr) {
struct LogData log;
//定位指针到文件开始位置
fseek(cfptr, 0L, SEEK_SET);
//读文件,将1个size大小的数据从文件中读出,传到log中
fread(&log, size, 1, cfptr);
printf("logid logdate lognote charge balance\n");
while (!feof(cfptr)) {
//输出数据
printf("%61d&-11s%-15s%10.2lf%10.2lf",
log.logid, log.logdate, log.lognote, log.charge, log.balance);
//再次开始读取
//为什么要一个一个的读取,不选择直接一次性读完,因为要换行,增加可读性
fread(&log,size, 1, cfptr);
}
}
//查询显示最后一条记录
void QueryLastLog(FILE* cfptr) {
struct LogData log;
long logcount;
logcount = getLogcount(cfptr);
//表示有过操作,有记录存在
if (logcount > 0) {
//读取最后一次记录
fseek(cfptr, size * (logcount - 1), SEEK_SET);
fread(&log, size, 1, cfptr);
printf("The Last log is:\n");
printf("logid:%-61d\nlogdate:%-11s\nlognote:%-15s\n",
log.logid, log.logdate, log.lognote);
printf("charge:%-10.2lf\nbalance:%-10.2lf\n",
log.charge, log.balance);
}
//如果没有过任何操作
else
printf("no logs in file!\n");
}
//添加新纪录
void AddNewLog(FILE* cfptr) {
struct LogData log, lastlog;
long logcount;
//初始化这次操作的数据
printf("Input logdate(format:2006-01-01):");
scanf("%s", log.logdate);
printf("Input lognote:");
scanf("%s", log.lognote);
printf("Input Charge:Income+and expend-:");
scanf("%lf", &log.charge);
//获取记录数
logcount = getLogcount(cfptr);
//若之前有过操作,
if (logcount > 0) {
fseek(cfptr, size * (logcount - 1), SEEK_SET);
//读出文件,并且修改数据
fread(&lastlog, size, 1, cfptr);
log.balance = lastlog.logid + 1;
log.balance = log.charge + lastlog.balance;
}
//如果是第一次操作
else {
log.logid = 1;
log.balance = log.charge;
}
//操作完之后,一定要将文件指针指向文件开头才能写文件
rewind(cfptr);
printf("logid=%ld\n", log.logid);
fwrite(&log, sizeof(struct LogData), 1, cfptr);
}
enum Choice {
addNewLog=1,
listAllLog,
queryLastLog
};
int main() {
FILE* fp;
int choice;
//if ((fp = fopen("cashbox.dat", openmode)) == NULL)
if ((fp = fopen("cashbox.dat", "r")) == NULL) {
printf("can not open file cashbox.dat!\n");
exit(0);
}
size = sizeof(struct LogData);
while ((choice = inputchoice()) != 0){
switch (choice) {
case addNewLog: AddNewLog(fp);
break;
case listAllLog:ListAllLog(fp);
break;
case queryLastLog:QueryLastLog(fp);
break;
default:printf("Input Error.");
break;
}
}
if (fclose(fp)) {
printf("can not close the file!\n");
exit(0);
}
return 0;
}
后面我会专门再写一章文件操作的标准函数篇,在文件操作中,这些标准函数的使用一定是要十分熟悉的。
同志们继续努力,革命还未成功呢!加油哦。