Day13.文件(下)
一、块读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main0101(void)
{
//char arr[16] = "hello world";
int arr[] = { 888888,888888,888888,888888 };
FILE* fp = fopen("a.txt", "wb");
if (!fp)
return -1;
fwrite(arr, sizeof(int), 4, fp);
fclose(fp);
return 0;
}
int main0102(void)
{
FILE* fp = fopen("a.txt", "rb");
if (!fp)
return -1;
int arr[10];
//fread(arr, sizeof(int), 4, fp);
//fread(arr, 1, 16, fp);
int i = 0;
while (!feof(fp))
{
fread(&arr[i++], sizeof(int), 1, fp);
}
fclose(fp);
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
printf("%d\n", arr[2]);
printf("%d\n", arr[3]);
return 0;
}
二、结构体写入二进制文件
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct students
{
char name[20];
int age;
char sex;
char tel[15];
};
typedef struct students stu;
int main0201(void)
{
stu s[5] = { {"徐",23,'M',"110"},
{"马",23,'W',"120"} ,
{"曹",24,'M',"130"} ,
{"段",24,'M',"140"} ,
{"邓",24,'M',"150"} };
FILE* fp = fopen("b.txt", "wb");
if (!fp)
return -1;
int i = 0;
for (i; i < 5; i++)
{
fwrite(&s[i], sizeof(stu), 1, fp);
}
fclose(fp);
return 0;
}
int main0202(void)
{
stu* s = (int*)malloc(sizeof(stu) * 5);
FILE* fp = fopen("b.txt", "rb");
if (!fp)
return -1;
int i = 0;
while (!feof(fp))
{
fread((s+i++), sizeof(stu), 1, fp);
}
fclose(fp);
for (i = 0; i < 5; i++)
{
printf("姓名:%s\t年龄:%d\t性别:%s\t电话:%s\n", (s + i)->name, (s + i)->age, (s + i)->sex == 'M' ? "男" : "女", (s + i)->tel);
}
free(s);
return 0;
}
三、获取文件字节大小
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(void)
{
//获取文件的字节大小,需要导入两个头文件
struct stat s;
stat("a.txt", &s);
printf("文件字节大小:%d\n", s.st_size);
return 0;
}
四、大文件拷贝(Linux下程序)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include<sys/types.h>
#include <sys/stat.h>
//10M大小
#define MAXSIZE 1024*1024*10
int main(int argc,char * argv[])
{
unsigned int start_time= time(NULL);
if(argc < 3)
{
printf("缺少参数\n");
return -1;
}
//mycp wow.2.mp4 wow.3.mp4
//argv[0] argv[1] arr[2]
FILE * fp1 = fopen(argv[1],"r");
FILE * fp2 = fopen(argv[2],"w");
if(!fp1 || !fp2)
{
printf("操作文件失败\n");
return -2;
}
//获取文件属性
struct stat *s = NULL;
//获取源文件
stat(argv[1],s);
char * ch;
int maxSize=0;
if(s->st_size < MAXSIZE)
{
maxSize =s->st_size;
ch = (char *)malloc(sizeof(char)*s->st_size);
}
else
{
maxSize = MAXSIZE;
ch = (char *)malloc(sizeof(char)*MAXSIZE);
}
while(!feof(fp1))
{
//memset(ch,0,maxSize);
int len = fread(ch,1,100,fp1);//123
fwrite(ch,1,len,fp2);
}
fclose(fp1);
fclose(fp2);
free(ch);
unsigned int end_time=time(NULL);
printf("花费时间:%d(s)\n",end_time-start_time);
return 0;
}
五、文件光标读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
fseek(文件流,移动字节,模式) 移动光标位置
模式:SEEK_SET 以文件开头为标准
SEEK_CUR 以文件光标当前位置为标准
SEEK_END 以文件结尾为标准
ftell(文件流) 获取当前光标位置 ,返回值是long类型,-1代表失败
rewind(文件流) 光标回到文件开头
*/
int main0401(void)
{
char* arr = "hello world";
FILE* fp = fopen("c.txt", "w");
if (!fp)
return -1;
fputs(arr, fp);
fclose(fp);
return 0;
}
int main0402(void)
{
FILE* fp = fopen("c.txt", "r");
if (!fp)
return -1;
//SEET_SET 文件起始位置
//fseek(fp, 6, SEEK_SET);
//SEEK_END 文件结尾位置
//fseek(fp, -5, SEEK_END);
char ch;
while ((ch = fgetc(fp)) != EOF)
{
//SEEK_CUR 光标当前位置
fseek(fp, 5, SEEK_CUR);
printf("%c", ch);
}
fclose(fp);
return 0;
}
int main0403(void)
{
FILE* fp = fopen("c.txt", "r");
if (!fp)
return -1;
char ch;
while ((ch = fgetc(fp)) != EOF)
{
//获取当前光标位置
int len = ftell(fp);
printf("%c", ch);
printf("%d\n", len);
}
return 0;
}
int main0404(void)
{
FILE* fp = fopen("c.txt", "r");
if (!fp)
return -1;
char ch;
for (int i = 0; i < 5; i++)
{
ch = fgetc(fp);
}
//将光标移动到文件开头
rewind(fp);
printf("%ld", ftell(fp));
return 0;
}
六、文件的删除和重命名
文件删除:
remove(文件路径文件名);
返回值:成功0;失败-1
文件重命名:
//移动操作
rename(原来文件路径文件名,新文件路径文件名);
返回值:成功0;失败-1
七、文件缓冲区
fflush(文件流)
//建议不要频繁操作硬盘