#include <stdio.h>
#include <string.h>
#include <windows.h>
//行刷新
void test01()
{
printf("hello world\n");
while(1);
}
//满刷新
void test02()
{
for (int i=0; i<100; i++)
{
printf("0123456789");
Sleep(2);
}
}
//强制刷新 fflush
void test03()
{
printf("0123456789");
fflush(stdout);
while(1);
}
//关闭刷新
void test04()
{
printf("0123456789");
}
//模拟时钟 Sleep
void test05()
{
int i = 0;
while (1)
{
printf("\r%02d:%02d", i/60, i%60);
fflush(stdout);
Sleep(1000);
i++;
}
}
//打开文件、关闭文件 fopen fclose
void test06()
{
FILE * fp = NULL;
//打开文件 fopen
fp = fopen("test.txt", "w");
if (fp == NULL)
{
perror("fopen");
return;
}
//关闭文件 fclose
if(fclose(fp) != 0)
{
perror("fclose");
return;
}
}
//一次读、写一个字符 fgetc fputc
void test07()
{
FILE * fp = NULL;
//打开文件 fopen
fp = fopen("test.txt", "r+");
if (fp == NULL)
{
perror("fopen");
return;
}
#if 1
//fgetc读字符
while (1)
{
char ch = fgetc(fp);
if (ch == EOF)
{
break;
}
printf("%c", ch);
}
#endif
#if 1
//fputc写字符
char* file_data = "hello world\n";
while (*file_data != '\0')
{
if(fputc(*file_data, fp) == EOF || fputc(*file_data, stdout) == EOF)
{
perror("fputc");
break;
}
file_data++;
}
#endif
fclose(fp);
}
//一次读、写一个字符串 fgets fputs
void test08()
{
FILE *fp_r = fopen("a.txt", "r");
if (fp_r == NULL)
{
perror("fopen");
return;
}
FILE *fp_w = fopen("b.txt", "w");
if (fp_w == NULL)
{
perror("fopen");
return;
}
while (1)
{
//读一行
char buf[128] = "";
char *ret = fgets(buf, sizeof(buf), fp_r);
if (ret == NULL)
{
break;
}
//写一行
fputs(buf, fp_w);
}
fclose(fp_r);
fclose(fp_w);
}
typedef struct
{
char name[16];
int atk;
int def;
}HERO;
//一次写n块文件数据 fwrite 内存数据的原样输出,返回值是实际写入的块数。
void test09()
{
HERO hero[] = {{"德玛", 60, 90}, {"盲僧", 80, 80}, {"小法", 90, 40}};
int n = sizeof(hero)/sizeof(hero[0]);
FILE * fp = fopen("hero.txt", "w");
if (fp == NULL)
{
perror("fopen");
return;
}
fwrite(hero, sizeof(HERO), n, fp);
fclose(fp);
}
//一次读n块文件数据 fread 磁盘数据的原样输入,返回值是实际读到的整数块。
void test10()
{
HERO hero[3];
memset(hero, 0, sizeof(hero));
FILE *fp = fopen("hero.txt", "r");
if (fp == NULL)
{
perror("fopen");
return;
}
fread(hero, sizeof(HERO), 3, fp);
for (int i=0; i<3; i++)
{
printf("%s %d %d\n", hero[i].name, hero[i].atk, hero[i].def);
}
fclose(fp);
}
//格式化写 fprintf
void test11()
{
HERO hero[] = {{"德玛", 60, 90}, {"盲僧", 80, 80}, {"小法", 90, 40}};
int n = sizeof(hero)/sizeof(hero[0]);
FILE * fp = fopen("hero.txt", "w");
if (fp == NULL)
{
perror("fopen");
return;
}
//fwrite(hero, sizeof(HERO), n, fp);
for (int i=0; i<n; i++)
{
fprintf(fp, "英雄:%s, 攻击力:%d, 防御力:%d。\n", hero[i].name, hero[i].atk, hero[i].def);
}
fclose(fp);
}
//格式化读 fscanf
void test12()
{
HERO hero[3];
memset(hero, 0, sizeof(hero));
FILE *fp = fopen("hero.txt", "r");
if (fp == NULL)
{
perror("fopen");
return;
}
//fread(hero, sizeof(HERO), 3, fp);
for (int i=0; i<3; i++)
{
fscanf(fp, "英雄:%[^,], 攻击力:%d, 防御力:%d。\n", hero[i].name, &hero[i].atk, &hero[i].def);
}
for (int i=0; i<3; i++)
{
printf("%s %d %d\n", hero[i].name, hero[i].atk, hero[i].def);
}
fclose(fp);
}
//文件顺序读写,读写才能移动流指针。
void test13()
{
FILE *fp = fopen("c.txt", "w+");
if (fp == NULL)
{
perror("fopen");
return;
}
//写
fputs("hello world\n", fp);
fclose(fp);
//读
fp = fopen("c.txt", "r");
char buf[128] = "";
fgets(buf, sizeof(buf), fp);
fclose(fp);
printf(buf);
}
//rewind 复位文件流指针
void test14()
{
FILE *fp = fopen("c.txt", "w+");
if (fp == NULL)
{
perror("fopen");
return;
}
//写
fputs("hello world\n", fp);
//复位文件流指针
rewind(fp);
//读
char buf[128] = "";
fgets(buf, sizeof(buf), fp);
fclose(fp);
printf(buf);
}
//ftell 返回文件流指针,距离文件首部的字节数
void test15()
{
FILE *fp = fopen("c.txt", "w+");
if (fp == NULL)
{
perror("fopen");
return;
}
//写
fputs("hello world\n", fp);
long len = ftell(fp);
printf("文件的大小:%ld\n", len);
fclose(fp);
}
//fseek 文件流指针定位,综合案例 一次读取文件数据。
void test16()
{
FILE *fp = fopen("d.txt", "r");
if (fp == NULL)
{
perror("fopen");
return;
}
//定位文件流指针到尾部
fseek(fp, 0, 2);
//获取文件的大小
long len = ftell(fp);
printf("文件大小:%ld\n", len);
//将文件流指针复位
rewind(fp);
//申请堆区空间,存放文件数据
unsigned char *buf = (unsigned char *)calloc(1, len+1);
fread(buf, len, 1, fp);
printf("%s", buf);
fclose(fp);
//释放堆区空间
if (buf != NULL)
{
free(buf);
buf = NULL;
}
}
int main(int argc, char const* argv[])
{
test16();
return 0;
}
C 语法 - 12_文件
于 2023-04-19 23:35:49 首次发布