Leer y escribir archivos en bloques
#include<stdio.h>
#include<string.h>
#pragma pack(show)
struct Hero
{
char name[64];
int age;
};
void test01()
{
int i;
FILE* f_read;
struct Hero temp[4];
//写文件
FILE* f_write = fopen("./test3.txt","wb");
struct Hero heros[]=
{
{"孙悟空",999},
{"鲁班",5},
{"达摩",79},
{"貂蝉",99}
};
if(f_write == NULL)
return;
for(i = 0;i < 4;i++)
{
fwrite(&heros[i],sizeof(struct Hero),1,f_write);
}
fclose(f_write);
//读文件
//FILE* f_read = fopen("./test1.txt","r");
f_read = fopen("./test3.txt","rb");
if(f_read == NULL)
return;
fread(temp,sizeof(struct Hero),4,f_read);
for(i = 0;i< 4;i++)
printf("姓名:%s 年龄: %d\n",temp[i].name ,temp[i].age);
fclose(f_read);
}
int main()
{
test01();
return 0;
}
Formateo para leer y escribir archivos
#include<stdio.h>
#include<string.h>
#pragma pack(show)
void test01()
{
int i;
char temp[1024] = {0};
FILE* f_read;
//写文件
FILE* f_write = fopen("./test4.txt","w");
if(f_write == NULL)
return;
fprintf(f_write,"hello world %s","abcd");
fclose(f_write);
//读文件
//FILE* f_read = fopen("./test1.txt","r");
f_read = fopen("./test4.txt","r");
if(f_read == NULL)
return;
while(!feof(f_read))
{
fscanf(f_read,"%s",temp);
printf("%s\n",temp);
}
fclose(f_read);
}
int main()
{
test01();
return 0;
}
resultado de la operación:
hola
mundo
abcd
por favor presione cualquier tecla para continuar ...
Anote este código:
while (! feof (f_read))
{ fscanf (f_read, "% s", temp); printf ("% s \ n", temp); }
Si lo cambia a:
while (! feof (f_read))
{ fscanf (f_read, "% s", temp); printf ("% s", temp); }
resultado de la operación:
helloworldabcd por favor presione cualquier tecla para continuar ...
Uso de rebobinado y búsqueda
#include<stdio.h>
#include<string.h>
#pragma pack(show)
struct Hero
{
char name[64];
int age;
};
void test01()
{
int i;
FILE* f_read;
struct Hero temp;
//写文件
FILE* f_write = fopen("./test3.txt","wb");
struct Hero heros[]=
{
{"孙悟空",999},
{"鲁班",5},
{"达摩",79},
{"貂蝉",99}
};
if(f_write == NULL)
return;
for(i = 0;i < 4;i++)
{
fwrite(&heros[i],sizeof(struct Hero),1,f_write);
}
fclose(f_write);
//读文件
//FILE* f_read = fopen("./test1.txt","r");
f_read = fopen("./test3.txt","rb");
if(f_read == NULL)
return;
//移动光标
//SEEK_SET 从开始 SEEK_END 从结尾 SEEK_CUR 从当前位置
//fseek(f_read,sizeof(struct Hero)*2,SEEK_SET);
fseek(f_read,-(int)sizeof(struct Hero)*2,SEEK_END); //这块不能直接-sizeof(...),因为sizeof的返回值为无符号类型,加个负号没有意义,所以前面给他强转成long、int都行
fread(&temp,sizeof(struct Hero),1,f_read);
printf("姓名:%s 年龄: %d\n",temp.name ,temp.age);
rewind(f_read); //将光标移到开始处
fread(&temp,sizeof(struct Hero),1,f_read);
printf("姓名:%s 年龄: %d\n",temp.name ,temp.age);
fclose(f_read);
}
int main()
{
test01();
return 0;
}
resultado de la operación:
Nombre: Bodhidharma Edad: 79
Nombre: Rey Mono Edad: 999
Por favor presione cualquier tecla para continuar ...
perror ()
#include<stdio.h>
#include<string.h>
#pragma pack(show)
struct Hero
{
char name[64];
int age;
};
void test01()
{
int i;
FILE* f_read;
//写文件
f_read = fopen("./test5.txt","rb");
if(f_read == NULL)
{
//error 宏
//printf("文件加载失败\n");
perror("文件加载失败"); //用户提示信息 + 系统提示信息
return;
}
}
int main()
{
test01();
return 0;
}
resultado de la operación:
Error al cargar el archivo: no
existe tal archivo o directorio. Presione cualquier tecla para continuar ...