二进制读写才是本质

二进制的读写对文件标记不敏感。

eg: 对图片进行加密与解密:

用命令的形式去执行:

//xx.exe -c src dest 加密

//xx.exe -d src dest 解密

他的参数就是argv[0-3]------使用Qt

#include<stdio.h>
#include<stdlib.h>
#include<string.h> void encode(char *buf,int n) {
for(int i = 0;i < n;i++) {
buf[i]++;
}
}
void decode(char *buf,int n) {
for(int i = 0;i < n;i++) {
buf[i]--;
}
}
int main(int argv,char *argv[]) {
if(argv != 4) {
printf("use xx.exe -d[-c] src dest\n");
exit(-1);
}
FILE *pfr = fopen(argv[2],"rb+");
if(pfr == NULL) {
exit(-1);
}
FILE *pfw = fopen(argv[3],"wb+");
if(pfw == NULL) {
fclose(pfr);
exit(-1);
}
int buf[1024];
int n;
if(strcmp(argv[1],"-c") == 0) {
while((n = fread((void *)buf,1,1024,pfr)) > 0) {
encode(buf,n);//加密函数
fwrite((void *)buf,1,n,pfw);
}
}else if(strcmpy(argc[1],"-d") == 0) {
while((n = fread((void *)buf,1,1024,pfr)) > 0) {
encode(buf,n);//加密函数
fwrite((void *)buf,1,n,pfw);
}
}else{
printf("arg error\n");
}
fclose(pfr);
fclose(pfw); return 0;
}

读写结构体的优势

结构体中的数据类型不统一,此时最适合用二进制的方式进行读写。二进制的接口可以读文本,而文本的接口不可以读二进制。

#include<stdio.h>
typedef struct student{
int num;
char name[30];
char sex;
float math;
float english;
float chinese;
}Stu; int main() {
Stu s[5] = {
{1001,"assassin",'f',89,99,100},
{1002,"wunworld",'f',99,89,79},
{1003,"intelwisd",'m',98,80,100},
{1004,"seafwg",'m',99,90,99},
{1005,"xxxx",'f',90,99,100}
};//把初始化的内容写进结构体中,读出来。
//把数据以二进制形式保存,文本原样保存
FILE * pfs = fopen("stu.data","w+");
if(pfs == NULL) {
exit(-1);
}
for(int i = 0;i < 5;i++) {
fwrite((void *)&s[i],sizeof(Stu),1,pfs);//注意&s[i],对数组取地址
}
fclose(pfs);
return 0;
}

为什么结构体采用fread/fwrite来都写?

1.结构体数据类型不统一

2.可以将二进制转化为文本,降低效率,占用多余的存储空间。

读文件:
#include<stdio.h>
typedef struct student{
int num;
char name[30];
char sex;
float math;
float english;
float chinese;
}Stu; int main() {
Stu s[5] = {
{1001,"assassin",'f',89,99,100},
{1002,"wunworld",'f',99,89,79},
{1003,"intelwisd",'m',98,80,100},
{1004,"seafwg",'m',99,90,99},
{1005,"xxxx",'f',90,99,100}
};//把初始化的内容写进结构体中,读出来。
//把数据以二进制形式保存,文本原样保存
FILE * pfw = fopen("stu.data","w+");
if(pfw == NULL) {
exit(-1);
}
Stu s;
while(fread((void *)&s,sizeof(Stu),1,pfw)) {
printf("num = %d\n",s.num);
printf("name = %s\n",s.name);
printf("sex = %c\n",s.sex);
printf("math = %.2f\n",s.math);
printf("english = %.2f\n",s.english);
printf("chinese = %.2f\n",s.chinese);
} fclose(pfw);
return 0;
}

假如以小空间来读大数据的情况,在while循环中使用循环,通过循环的小标逐渐读出数据

#include<stdio.h>
typedef struct student{
int num;
char name[30];
char sex;
float math;
float english;
float chinese;
}Stu; int main() {
Stu s[5] = {
{1001,"assassin",'f',89,99,100},
{1002,"wunworld",'f',99,89,79},
{1003,"intelwisd",'m',98,80,100},
{1004,"seafwg",'m',99,90,99},
{1005,"xxxx",'f',90,99,100}
};//把初始化的内容写进结构体中,读出来。
//把数据以二进制形式保存,文本原样保存
FILE * pfw = fopen("stu.data","w+");
if(pfw == NULL) {
exit(-1);
}
Stu s[3];//有3个,结构体有5个
while(fread((void *)&s,sizeof(Stu),1,pfw)) {
for(int i = 0;i < n;i++) {
printf("num = %d\n",s[i].num);
printf("name = %s\n",s[i].name);
printf("sex = %c\n",s[i].sex);
printf("math = %.2f\n",s[i].math);
printf("english = %.2f\n",s[i].english);
printf("chinese = %.2f\n",s[i].chinese);
}
} fclose(pfw);
return 0;
}

实践:将链表作为内存数据模型,见文件作为数据库,将终端作为交互界面。读文件生成链表,修改链表写入文件。

//1.初始化数据库,此时的数据库是文件
//2.都数据库,生成内存数据模型,链表
//3.增,查,改,删,排序
//4.更新数据库
typedef struct student
{
char name[30];
char sex;
int age;
float score;
}Stu;
typedef struct _StuNode
{
Stu data;
struct _StuNode *next;
}StuNode; void initData2File()
{
Stu s[4] =
{
{"assassin",'f',30,100},
{"wunworld",'f',27,79},
{"intelwisd",'m',25,100},
{"seafwg",'m',23,99},
{"xxxx",'f',23,100}
}
FILE *pf = fopen("stu.data","w+");
if(NULL == pf)
exit(-1);
fwrite((void *)s,sizeof(s),1,pf);
fclose(pf); return ;
} StuNode *createListFromFile(char *filePath)
{
FILE *pf = fopen(filePath,"r+");//格式写正确不然一不小心会覆盖
if(NULL == pf)
exit(-1);
StuNode *head = (StuNode *)malloc(sizeof(StuNode));
head->next = NULL; StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
while(fread((void *)&cur->data,sizeof(Stu),1,pf) )
{
cur->next = head->next;
head->next = cur; cur = (StuNode *)malloc(sizeof(StuNode));
}
free(cur);
return head;
}
void traverseStuList(StuNode *head)
{
printf("name\t\t\tsex\t\tage\t\tscore\n");
head = head->next;
while(head)
{
printf("%-10s\t\t%c\t\t%d\t\t%.2f\n",head->data.name,head->data.sex,head->data.age,head->data.score);
head = head->next;
}
} void addListStuNode(StuNode *head)
{
StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
printf("name:\n");
scanf("%s",cur->data.name); getchar();
printf("sex:\n");
scanf("%c",&cur->data.sex); getchar();
printf("age:\n");
scanf("%sd",&cur->data.age); getchar();
printf("score:\n");
scanf("%f",c&ur->data.score); cur->next = head->next;
head->next = cur;
} StuNode * searchListStu(StuNode *head)
{
char name[30];
printf("pls input your search name:");
scanf("%d",name);
head = head->next;
while(head)
{
if(strcmp(head-data.name,name) == 0)
break;
head - head->next;
}
return head;
}
//删除操作
void deleteListNodeStu(StuNode *head)
{
StuNode *pfind = searchListStu(head);
if(pfind == null)
{
printf("你所要删除的人不存在!\n");
return ;
}
while(head->next != pfind)
{
head = head->next;//找到前驱
}
head->next = pfind->next;
free(pfind);
return ;
}
//
int lenListStu(StuNode *head)
{
int len;
head = head->next;
while(head)
{
len++;
head = head->next;
}
return len;
}
void sortListStu(StuNode *head)
{
int len = lenListStu(head);
StuNode *prep,*p,*q;
for(int i = 0;i < len-1;i++)
{
prep = head;
p = prep->next;
q = q->next;
for(int j = 0;j < len-1-i;j++)
{
if(strcmp(p->data.name,q->data.name) > 0)
{
prep->next = q;
p->next = q->next;
q->next = p; prep = q;
q = p->next;
continue;
}
prep = prep->next;
p = p->next;
q = q->next;
}
}
}
void saveList2FileStu(StuNode *head,char *filePath)
{
FILE *pf = fopen(filePath,"w+");
if(NULL == pf)
exit(-1);
head = head->next;
while(head)
{
fwrite((void *)&head->data,sizeof(Stu),1,pf);
head =head->next;
}
fclose(pf);
}
void destoryListStu(head)
{
StuNode *t;//找个替身
while(head)
{
t = head;
head = head->next;
free(t);
}
}
int main()
{
//initdata2File();
StuNode *head = createListFromFile("stu.data");
/*
traverseStuList(head);
printf("1->add\t 2->search\t 3->delete\t4->exit\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1:
addListStuNode(head);
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
printf("你输入错误!\n");
}*/ StuNode *pfind;
while(1)
{
system("cls");//系统清屏
traverseStuList(head);
printf("1->add\t 2->search\t 3->delete\t4->exit\n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1:
addListStuNode(head);
break;
case 2:
if(pfind = searchListStu(head))
{
printf("%-10s\t\t%c\t\t%d\t\t%.2f\n",pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.score); }
else
{
printf("没有此人!\n");
}
break;
case 3:
deleteListNodeStu(head);
break;
case 4:
sortListStu(head);
break;
case 5:
saveList2FileStu(head,"stu.data");
destoryListStu(head);
return 0;
default:
printf("你输入错误!\n");
}
}
}

文件偏移

int main()
{
FILE *pf = fopen("xxx.txt","w=");
fputs("abcdefg",pf);
int n = ftell(pf);//
printf("n = %d\n",n);//n = 7,ftell(),求字节的大小
rewind(pf);//将文件重新指针向一个流的开头
n = ftell(pf);
printf("n = %d\n",n);//0 feek(pf,0,SEEK_END);//操作文件pf,从SEEK_END末尾偏移0个单位。
n = ftell(pf);
printf("n = %d\n",n);//7 feek(pf,0,SEEK_SET);//从头偏移0个
n = ftell(pf);
printf("n = %d\n",n);//0 feek(pf,1,SEEK_CUR);//从当前位置偏移一个单位
n = ftell(pf);
printf("n = %d\n",n);//0 return 0;
}

C/C++(文件操作二)的更多相关文章

  1. Node.js文件操作二

    前面的博客 Node.js文件操作一中主要是对文件的读写操作,其实还有文件这块还有一些其他操作. 一.验证文件path是否正确(系统是如下定义的) fs.exists = function(path, ...

  2. 【Directory】文件操作(初识文件操作二)

    上篇我们说了关于文件的创建删除更改可以通过File这个类来完成.对于目录的操作其实File类也可以完成创建删除等相关的操作.用法跟文件的方法大致相同. 那么下面就一起来看一下关于目录相关的用法. 一, ...

  3. 基于VC的声音文件操作(二)

    (二)VC的声音操作 操作声音文件,也就是将WAVE文件打开获取其中的声音数据,根据所需要的声音数据处理算法,进行相应的数学运算,然后将结果重新存储与WAVE格式的文件中去:可以使用CFILE类来实现 ...

  4. DAY8 文件操作(二)

    一.写 1.1写文件 # w:没有文件新建文件,有文件就清空文件 w = open('1.txt', 'w', encoding='utf-8') w.write('000\n') # 在写入大量数据 ...

  5. Java文件操作二:File文件的方法

    一.文件的判断方法 判断方法 .boolean canExecute()判断文件是否可执行 .boolean canRead()判断文件是否可读 .boolean canWrite() 判断文件是否可 ...

  6. ObjectiveC 文件操作二

    10,文件委托,以便操作文件.头部看起来像是这样. @interface MyFileManager : NSObject @property(strong)NSFileManager *fileMa ...

  7. AIR文件操作(二):使用文件对象操作文件和目录

    转载于:http://www.flashj.cn/wp/air-file-operation2.html 文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在A ...

  8. Python 文件操作二

    readlines就像read没有参数时一样,readlines可以按照行的方式把整个文件中的内容进行一次性读取,并且返回的是一个列表,其中每一行的数据为一个元素 #coding=utf-8 f = ...

  9. iOS学习之iOS沙盒(sandbox)机制和文件操作(二)

    1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...

随机推荐

  1. 解决高版本vm打开虚拟机报错

    问题: 打开虚拟机的文件目录,找到.vmx 文件 用记事本打开重命名后的“.vmx.txt”文件 找到行:policy.vm.mvmtid = "52 10 08 ed ff 34 ed d ...

  2. 新机器的vim配置

    最近一直用vim去写acm代码,算是一种练习吧. 用着用着感觉不错,最近也稍微配置了一下vim,用着更舒服了 键盘映射 ESC<->CapsLock 我们知道vim有自带的键盘映射命令,但 ...

  3. 紫书 习题 10-11 UVa 1646(斐波那契+高精度)

    自己用手算一下可以发现是斐波那契数列,然后因为数字很大,用高精度 以后做题的时候记得算几个数据找规律 #include<cstdio> #include<cmath> #inc ...

  4. Xwiki平台Windows搭建(Tomcat7 + XWiki6.2 + MySQL5.5)

    背景介绍 国内xwiki安装使用资料较少,根据自己使用xwiki经验,总结出来,供参考,同时希望感兴趣的朋友能够一起讨论,XWiki是一个强大的Java开源的Wiki引擎. 它支持一些受欢迎的特性如: ...

  5. Android 4.3 系统裁剪——删除不使用的app及添加自己app

    删除不使用的apk 系统自带的app位置是在/android4.3/packages/apps 以下是一些APP作用分析: | |– BasicSmsReceiver | |– Bluetooth ( ...

  6. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

  7. 开源3D游戏引擎Irrlicht简介

    Irrlicht简介 Irrlicht在国内也被叫做"鬼火"引擎,是一款用C++编写的开放源代码的高性能游戏引擎.而且是跨平台的,具有很好的移植性,Irrlicht支持OpenGl ...

  8. Sql Server通用分页存储过程

    Sql Server2005通用分页存储过程 CREATE PROCEDURE [dbo].[Common_GetPagedList] ( @TableName nvarchar(100), --表名 ...

  9. DOM操作系列-01

    ]常见事件: //onclick     点击时触发事件. //ondblclick    双击时触发事件. //onkeydown   按键按下 //onkeypress   点击按键 //onke ...

  10. 利用Python网络爬虫抓取微信好友的所在省位和城市分布及其可视化

    前几天给大家分享了如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,感兴趣的小伙伴可以点击链接进行查看.今天小编给大家介绍如何利用Python网络爬虫抓取微信好友的省位和城市,并且将 ...