标签:style blog http color io os ar for 文件
fseek(文件指针,偏移量,基准); //偏移量的单位是字节
函数执行成功返回0,执行失败返回一个非0值
注意下面的特殊现象:
返回值是0 fseek(p,2,SEEK_END): 到终点了往后跳是成功的
返回值是-1 fseek(p,-2,SEEK_SET);在起始点往前面跳非法
windows的 b 模式 不影响 \r\n的偏移,只会按真实存在的字节偏移
ftell(p) 返回当前的文件指针位置
#include <stdio.h>
int main()
{
FILE *fp = fopen("a.txt","r+");
if(!fp)
{
printf("打开文件失败\n");
return -1;
}
int num = fseek(fp,2,SEEK_SET);
printf("fseek = %d\n",num);
printf("ftell = %ld\n",ftell(fp));
char buf[1024];
while(fgets(buf,sizeof(buf),fp) != NULL)
{
printf("%s\n",buf);
}
return 0;
}
feof的一个特殊注意点:
#include <stdio.h>
int main(void)
{
FILE *fp = fopen("a.txt","r");
while(!feof(fp))
{
char tmp = 0;
fread(&tmp,sizeof(tmp),1,fp);
printf("%x\n",tmp);
}
fclose(fp);
return 0;
}
abc\n 后面多出一个0 是因为feof到了文件末尾并不返回1,只有越过了文件末尾才返回1。即以上的fread多读了一次,多输出了一个0。注意:在linux上统计行数也会多出一行。解决方案:能不用feof就不用feof
#include <stdio.h>
int main(void)
{
FILE *fp = fopen("a.txt","r");
char tmp = 0;
while(fread(&tmp,sizeof(tmp),1,fp) > 0)
{
printf("%x\n",tmp);
}
fclose(fp);
return 0;
}
fread fwrite(重点):
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
fread()会返回实际读入的nmemb数目。fwrite()会返回实际写入的nmemb数目。
fread 和 fwrite拷贝一个二进制文件
这样拷贝是不行的,b.pdf永远都会是1024的倍数
#include <stdio.h>
int main(void)
{
FILE *fp1 = fopen("a.pdf","r");
FILE *fp2 = fopen("b.pdf","w");
char buf[1024];
while(fread(buf,sizeof(buf),1,fp1) > 0)
{
fwrite(buf,sizeof(buf),1,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
解决方案:
#include <stdio.h>
int main(void)
{
FILE *fp1 = fopen("a.pdf","r");
FILE *fp2 = fopen("b.pdf","w");
char buf[1024];
int len = 0;
while((len = fread(buf,1,sizeof(buf),fp1)) > 0) //fread返回实际读入的第二个参数的数量
{
fwrite(buf,1,len,fp2);
//我读了多少个字节,就写多少个字节}
fclose(fp1);
fclose(fp2);
return 0;
}
对二进制文件进行修改,检索,插入等操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int ID;
char name[20];
};
void init_student(char *filename)
{
struct student st[10] = {
{1,"刘德华"},
{2,"周杰伦"},
{3,"成龙"},
{4,"范冰冰"},
{5,"柳岩"},
{6,"杨幂"},
{7,"林志玲"},
{8,"韩寒"},
{9,"普京"},
{10,"赵本山"},
};
FILE *fp = fopen(filename,"w");
if(!fp)
{
printf("打开文件失败\n");
return;
}
fwrite(st,sizeof(struct student),10,fp);
fclose(fp);
}
void show_student(char *filename) //循环输出所有学员信息
{
FILE * fp = fopen(filename,"r");
if(!fp)
{
printf("打开文件失败\n");
return;
}
struct student st;
memset(&st,0,sizeof(st));
while(fread(&st,sizeof(st),1,fp) > 0)
{
printf("%d:%s\n",st.ID,st.name);
}
fclose(fp);
printf("***************\n");
}
void update_student(char * filename ,int n) //对二进制文件进行修改
{
FILE * fp = fopen(filename,"r+");
if(!fp)
{
printf("打开文件失败\n");
return;
}
fseek(fp,(n-1)*sizeof(struct student),SEEK_SET);
struct student st = {100,"刘威"};
fwrite(&st,sizeof(st),1,fp);
fclose(fp);
}
void find_student(char *filename)
//对二进制文件进行检索{
FILE * fp = fopen(filename,"r");
if(!fp)
{
printf("打开文件失败\n");
return;
}
int n = 0;
printf("你输入你需要查找的学生编号:");
scanf("%d",&n);
fseek(fp,(n-1)*sizeof(struct student),SEEK_SET);
struct student st;
memset(&st,0,sizeof(st));
fread(&st,sizeof(st),1,fp);
printf("%d:%s\n",st.ID,st.name);
fclose(fp);
}
void add_student(char *filename , int n) //对二进制文件进行插入
{
FILE * fp = fopen(filename,"r");
if(!fp)
{
printf("打开文件失败\n");
return;
}
struct student *p = calloc(100,sizeof(struct student));
int index = 0,i = 0;
while(fread(&p[index],sizeof(struct student),1,fp) > 0)
index++;
for(i = index ; i >= n ; i--)
p[i+1] = p[i];
p[n].ID = 88;
strcpy(p[n].name,"习jin平");
fclose(fp);
fp = fopen(filename,"w");
if(!fp)
{
printf("打开文件失败\n");
return;
}
fwrite(p,sizeof(struct student),index+1,fp);
fclose(fp);
}
int main(void)
{
init_student("a.bin");
show_student("a.bin");
//update_student("a.bin",4);
add_student("a.bin",5);
show_student("a.bin");
//find_student("a.bin");
return 0;
}
把本班的同学读入到结构体数组里,加上序号,并且排序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct student
{
int ID;
char name[20];
};
void show_student()
{
FILE *fp = fopen("student.bin","r");
while(!fp)
{
printf("打开文件失败\n");
return;
}
struct student st;
memset(&st,0,sizeof(st));
while(fread(&st,sizeof(st),1,fp) > 0)
{
printf("%d:%s\n",st.ID,st.name);
}
}
void sort_student()
{
FILE *fp = fopen("student.bin","r");
while(!fp)
{
printf("打开文件失败\n");
return;
}
struct student st[50];
int index = 0;
while(fread(&st[index],sizeof(struct student),1,fp) > 0)
index++;
int i,j,min;
struct student tmp;
for(i = 0 ; i < index ;i++)
{
min = i;
for(j = i+1; j < index ; j++)
if(st[min].ID > st[j].ID)
min = j;
if(min != i)
{
tmp = st[min];
st[min] = st[i];
st[i] = tmp;
}
}
fclose(fp);
fp = fopen("student.bin","w");
for(i = 0 ; i < index; i++)
fwrite(&st[i],sizeof(struct student),1,fp);
fclose(fp);
}
int main(void)
{
srand((unsigned)time(NULL));
FILE *fp1 = fopen("student.txt","r");
FILE *fp2 = fopen("student.bin","w");
while(!fp1 || !fp2)
{
printf("打开文件失败\n");
return -1;
}
char buf[1024];
struct student st;
memset(&st,0,sizeof(st));
int arr[42] = {0};
int tmp;
while(fgets(buf,sizeof(buf),fp1) != NULL)
{
if(buf[strlen(buf)-1] == ‘\n‘)
buf[strlen(buf)-1] = ‘\0‘;
tmp = rand()%42;
while(arr[tmp] == 1)
tmp = rand()%42;
arr[tmp] = 1;
st.ID = tmp;
strcpy(st.name,buf);
fwrite(&st,sizeof(st),1,fp2);
}
fclose(fp1);
fclose(fp2);
sort_student();
show_student();
return 0;
}
标签:style blog http color io os ar for 文件
原文地址:http://www.cnblogs.com/l6241425/p/3967484.html