011_C基础文件操作_练习
1.强化训练:实现vi、cat命令
实现cat命令
#include <stdio.h>
int main(int argc,char *argv[])
{
//打开文件,文件路径,argv[1],读方式
FILE *fp = fopen(argv[1],"r");
//读取文件内容,将内容显示到屏幕
char ch;
while (1)
{
ch = fgetc(fp);
if (feof(fp)) //如果文件到结尾,退出循环
{
break;
}
printf("%c", ch);
}
//关闭文件
fclose(fp);
return 0;
}
实现vi命令
#include <stdio.h>
#include <string.h>
int main(int argc , char *argv[])
{
//1 打开文件,文件路径,argv[1],读方式
FILE *fp = fopen(argv[1],"w");
//2 输入 写入到文件
char buf[1024];
while (1)
{
//默认遇到换行符,此处读取结束,换行符放在buf中
//fgets()按行读取
//从键盘读取内容fgets(),放在字符数组
fgets(buf, sizeof(buf), stdin);
//如果输入内容为“:wq”则结束
if (strncmp(buf, ":wq", 3) == 0)
{
break;
}
int i = 0;
while (buf[i] != '\0') //读取的内容没有到结束符,读取到的内容写入文件
{
fputc(buf[i], fp);
i++;
}
}
//3 关闭文件
fclose(fp);
return 0;
}
2.强化训练:文件版四则运算
有个文件大小不确定,每行内容都是一个四则运算表达式,还没有算出结果,写一个程序,自动算出其结果后修改文件。
#include <stdio.h>
#include <string.h>
int write_file()
{
//打开文件
FILE *fp = fopen("1.txt","w");
//写入文件
fputs("10+10=\n", fp);
fputs("10-10=\n", fp);
fputs("10*10=\n", fp);
fputs("10/10=\n", fp);
fputs("10+5=\n", fp);
fputs("10-5=\n", fp);
fputs("10*3=\n", fp);
fputs("10/2=\n", fp);
//关闭文件
fclose(fp);
}
int calc(int a, char ch, int b)
{
switch (ch)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return 0;
}
return 0;
}
int read_file()
{
//打开文件
FILE *fp = fopen("1.txt", "r");
//读文件
char buf[1024];
char tmp[1024] = { 0 };
while (1)
{
memset(buf, 0, sizeof(buf));
//遇到\n, 文件结束符,出错,结束本次读取
fgets(buf, sizeof(buf), fp);
if (strlen(buf) > 0)
{
//printf("buf = %s", buf);
int a, b;
char ch;
sscanf(buf, "%d%c%d=\n", &a, &ch, &b);
sprintf(buf, "%d%c%d=%d", a, ch, b, calc(a, ch, b));
//printf("buf = $%s$", buf);
strcat(tmp,buf);
}
//如果文本结束,跳出循环
if (feof(fp))
{
break;
}
}
//printf("%s", tmp);
//关闭文件
fclose(fp);
//关闭文件后,重新以w方式打开文件,目的清空文件,重新写内容
fp = fopen("1.txt", "w");
fputs(tmp, fp);
fclose(fp);
}
int main(int argc, char *argv[])
{
write_file();
read_file();
return 0;
}
3.强化训练:文件版排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAX 500
void write_file()
{
FILE *fp = fopen("test.txt", "w");
if (fp == NULL) //打开失败
{
perror("write_file fopen");
return;
}
//生成随机数据
srand((unsigned int)time(NULL));
int i = 0;
int num;
char buf[100];
for (i = 0; i < MAX; i++)
{
num = rand() % 100; //随机数控制在100以内
//---------方法一,二-------------//
//格式化一个字符串
sprintf(buf, "%d\n", num);
//往文件写入内容
fputs(buf, fp);
}
//关闭文件
fclose(fp);
fp = NULL;
}
void read_file()
{
FILE *fp = fopen("test.txt", "r"); //以读方式打开
if (fp == NULL) //打开失败
{
perror("read_file fopen");
return;
}
int a[1024];
int i = 0;
char buf[100];
int num;
while (1)
{
//读取一行内容,放在buf中,包含\n也读取进去
fgets(buf, sizeof(buf), fp);
if (feof(fp))//如果文件到结尾,跳出循环
{
break;
}
sscanf(buf, "%d\n", &num);
a[i] = num;
i++;
}
int n = i; //数组元素个数
int j = 0;
//数组排序
int tmp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
-----------方法一-----------------------/
#if 1
char dst[4 * 1024] = { 0 };
char src[512];
for (i = 0; i < n; i++)
{
//printf("%d, ", a[i]);
sprintf(src, "%d\n", a[i]);
//printf("src = %s", src);
strcat(dst, src);
}
fclose(fp);
fp = NULL;
//关闭文件后,重新写文件
fp = fopen("test.txt", "w");
fputs(dst, fp);
fclose(fp);
#endif
-----------方法二-----------------------/
#if 0
//关闭文件
fclose(fp);
fp = NULL;
//关闭文件,重新写文件
fp = fopen("test.txt", "w");//清空
fclose(fp);
fp = fopen("test.txt", "a");//追加
char dst[4*1024] = {0};
char src[512];
for(i = 0; i < n; i++)
{
//printf("%d, ", a[i]);
sprintf(src, "%d\n", a[i]);
fputs(src, fp);
}
fclose(fp);
#endif
}
int main(int argc, char *argv[])
{
write_file();
read_file();
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 500
#include <string.h>
void write_file()
{
FILE *fp = NULL;
//以写方式打开文件
fp = fopen("./6.txt", "w");
if(fp == NULL) //打开失败
{
perror("write_file fopen");
return;
}
//设置随机种子
srand( (unsigned int)time(NULL) );
int i = 0;
int num;
char buf[100];
for(i = 0; i < MAX; i++)
{
num = rand() % 100; //随机数控制在100以内
//-----------方法三--------------//
//格式化一个字符串
fprintf(fp, "%d\n", num);
}
//关闭文件
fclose(fp);
fp = NULL;
}
void read_file()
{
FILE *fp = NULL;
//以读方式打开文件
fp = fopen("./6.txt", "r");
if(fp == NULL) //打开失败
{
perror("write_file fopen");
return;
}
int a[1024];
int i = 0;
char buf[100];
int num;
while(1)
{
//-----------方法三--------------//
fscanf(fp, "%d\n", &num);
a[i] = num;
i++;
if( feof(fp) )//如果到文件结尾,跳出循环
{
break;
}
}
int n = i; //数组元素个数
int j = 0;
printf("n = %d\n", n);
//数组排序
int tmp;
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-1-i; j++)
{
if(a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
//关闭文件
fclose(fp);
fp = NULL;
//关闭文件,重新写文件
fp = fopen("6.txt", "w");//清空
char src[512];
for(i = 0; i < n; i++)
{
//-----------方法三--------------//
//printf("%d, ", a[i]);
fprintf(fp, "%d\n", a[i]);
}
fclose(fp);
}
int main(int argc, char *argv[])
{
write_file();
read_file();
return 0;
}
4.强化训练:大文件拷贝
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("err: ./a.out src dst\n");
return 0;
}
//1、打开源文件 argv[1] r
FILE *rFp = fopen(argv[1], "r");
//2、打开目的文件 argv[2] w
FILE *wFp = fopen(argv[2], "w");
//循环操作
//从源文件读取内容,再把内容写入到目的文件,读多少写多少
char buf[4*1024];
int len;
while(1)
{
len = fread(buf, 1, sizeof(buf), rFp);
printf("len = %d\n", len);
if(len == 0)
{
break;
}
fwrite(buf, 1, len, wFp);
}
//关闭文件
fclose(rFp);
fclose(wFp);
return 0;
}
#include <stdio.h>
typedef struct Student
{
int age;
char name[50];
int score;
}Student;
int main(int argc, char *argv[])
{
//打开文件
FILE *fp = fopen("7.txt", "r");
//读文件
Student s[10];
//&s[0], 放置读取文件内容的变量地址
//sizeof(Student), 读取文件内容的块大小
//4, 读取文件内容的块数目,读取文件的大小为:块大小*块数目
//fp, 操作的文件
//返回值:成功时值为读取文件内容的块数目,不是文件总大小
//失败返回0
//int ret = fread(&s[0], sizeof(Student), 4, fp);
//int ret = fread(&s[0], sizeof(s), 4, fp);
int ret = fread(&s[0], sizeof(Student), 40, fp);
printf("ret = %d\n", ret);
int i = 0;
int n = 4;
for(i = 0; i < n; i++)
{
printf("%d, %s, %d\n", s[i].age, s[i].name, s[i].score);
}
//关闭文件
fclose(fp);
return 0;
}
int main02(int argc, char *argv[])
{
//打开文件
FILE *fp = fopen("7.txt", "w");
//写文件
Student s[4] = {
18, "mike", 59,
22, "jiang", 66,
33, "lily", 77,
44, "lucy", 88
};
//s, 需要往文件写内容的变量首地址
//sizeof(s), 往文件写入内容的块大小
//1, 往文件写入内容的块数目,写入文件的大小为:块大小*块数目
//fp, 操作的文件
//返回值:成功时值为写入内容的块数目,不是文件总大小
//int ret = fwrite(s, sizeof(s), 1, fp);
int ret = fwrite(&s[0], 1, sizeof(s), fp);
printf("ret = %d\n", ret);
//关闭文件
fclose(fp);
return 0;
}
int main01(int argc, char *argv[])
{
//打开文件
FILE *fp = fopen("7.txt", "w");
//写文件
Student s1 = {18, "mike", 59};
//int ret = fwrite(&s1, sizeof(Student), 1, fp);
//int ret = fwrite(&s1, 1, sizeof(Student), fp);
//&s1, 需要往文件写内容的变量首地址
//15, 往文件写入内容的块大小
//4, 往文件写入内容的块数目,写入文件的大小为:块大小*块数目 = 15*4 = 60
//fp, 操作的文件
//返回值:成功时值为写入内容的块数目,不是文件总大小
int ret = fwrite(&s1, 15, 4, fp);
printf("ret = %d, sizeof(Student) = %lu\n", ret, sizeof(Student));
//关闭文件
fclose(fp);
return 0;
}