文件IO fgets fputs fgetc fwrite fread
fopen函数mode模式:
w+不是追加写 是多了一个读权限
文件指针+1没有意义
拷贝一个文件: fgets fputs (fgetc同理)
int main()
{
FILE *fp, *fpcp;
fp = fopen("yesteday_once_more.txt", "r");
fpcp = fopen("a", "w");
char buf[1024]; //一行给他1024个字节 尽量够用
while (fgets(buf, sizeof(buf), fp) != NULL)
{
fputs(buf, fpcp);
}
fclose(fp);
fclose(fpcp);
return 0;
}
获取用户键盘输入:
char buf[1024];
fgets( buf , sizeof(buf) , stdin );
fputs( buf , stdout);
把用户输入的记录到文件里,stdin就是键盘的输入
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("record", "a+");
char buf[10];
while (fgets(buf, sizeof(buf), stdin) != NULL)
{
fputs(buf,stdout);
fputs(buf, fp); //linux下 ctrl+d才生效
}
fclose(fp);
return 0;
}
统计一个文件有多少行: fgets
int line(char * s)
{
FILE *fp;
fp = fopen(s, "r");
char buf[1024];
int n = 0;
while (fgets(buf, sizeof(buf), fp) != NULL)
n++;
fclose(fp);
return n;
}
int main()
{
int n = line("yesteday_once_more.txt");
printf("%d\n", n);
}
统计一个文件有多少个字节: fgetc
int len(char * s)
{
FILE * fp;
int n = 0;
fp = fopen(s, "r");
if (!fp)
return -1;
char ch;
while ((ch = fgetc(fp)) != EOF)
n++;
fclose(fp);
return n;
}
int main()
{
int n = len("yes.txt");
printf("%d\n", n);
return 0;
}
fread的说明:
如果此函数用来拷贝,步长大了,容易丢失数据
长处在于读写结构体。
写入的是二进制文件 类似序列化的理念,更强大
fread读到末尾返回0 fgets 读到末尾返回NULL fgetc读到末尾返回 EOF [-1]
从一个文本文件读到结构体数组里,然后再fwrite到二进制文件,再fread到结构体数组
并增加了排序,用了函数指针
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct STU
{
int id;
char name[20];
char sex;
int score;
} *pS;
int count_stu(FILE * fp)
{
char buf[1024];
int n = 0;
while (fgets(buf, sizeof(buf), fp) != NULL)
n++;
rewind(fp);
return n / 5;
}
int file_stu(FILE * fp, pS a, int n)
{
int i = 0;
char buf[1024];
for (i = 0; i < n; i++)
{
fgets(buf, sizeof(buf), fp);
a[i].id = atoi(buf);
fgets(a[i].name, sizeof(a[i].name), fp);
strtok(a[i].name, "\n");
fgets(buf, sizeof(buf), fp);
a[i].sex = buf[0];
fgets(buf, sizeof(buf), fp);
a[i].score = atoi(buf);
fgets(buf, sizeof(buf), fp);
}
rewind(fp);
return i;
}
void show_stu(pS a, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%d %s %c %d\n", a[i].id, a[i].name, a[i].sex, a[i].score);
}
printf("***************\n");
return;
}
//此函数第一种写法
int stu_bin(FILE * fp, pS a, int n)
{
fwrite(a, sizeof(*a), n, fp);
rewind(fp);
return 0;
}
//此函数第二种写法
int stu_bin(FILE *fp, pS a, int n)
{
int i;
for (i = 0; i < n; i++)
{
fwrite(&a[i], sizeof(a[i]), 1, fp);
}
rewind(fp);
return i;
}
int bin_stu(FILE * fp, pS a, int n)
{
fread(a, sizeof(*a), n, fp);
rewind(fp);
return 0;
}
int sort_by_id(pS a, pS b)
{
return a->id - b->id;
}
int sort_by_name(pS a, pS b)
{
return strcmp(a->name, b->name);
}
int sort_by_sex(pS a, pS b)
{
return a->sex - b->sex;
}
int sort_by_score(pS a, pS b)
{
return a->score - b->score;
}
void sort_stu(pS a, int n, int(*fun)(pS a, pS b))
{
int i, j, min;
struct STU temp;
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (fun(&a[min], &a[j])>0)
{
min = j;
}
}
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
return;
}
int main(void)
{
FILE * fp = fopen("stu.txt", "r");
FILE * fp_bin = fopen("stu.bin", "w+"); //注意这里是w+,如果是w的话就不能读了,注意坑
if (!fp || !fp_bin)
return -1;
int count = count_stu(fp);
struct STU * a = (pS)malloc(count * sizeof(*a));
file_stu(fp, a, count);
//show_stu(a, count);
stu_bin(fp_bin, a, count);
memset(a, 0, (count * sizeof(*a)));
bin_stu(fp_bin, a, count);
fclose(fp);
fclose(fp_bin);
show_stu(a, count);
sort_stu(a, count, sort_by_id);
show_stu(a, count);
sort_stu(a, count, sort_by_name);
show_stu(a, count);
sort_stu(a, count, sort_by_sex);
show_stu(a, count);
sort_stu(a, count, sort_by_score);
show_stu(a, count);
free(a);
}
去除字符串的\n :
一个是
strtok(a[i].name, "\n");
另外一个是,
len = strlen(buf);
buf[len - 1] = '\0';
附件列表