标签:style blog http color os io ar for 文件
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;
}
#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;
}
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);
}
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;
}
#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);
}
8.5 文件IO fgets fputs fgetc fwrite fread
标签:style blog http color os io ar for 文件
原文地址:http://www.cnblogs.com/l6241425/p/3952321.html