1.使用fread和fwrite方式完成任意普通文件的拷贝功能。
模拟 文件下载
#include <stdio.h>
void do_copy(FILE *fp_s,FILE *fp_d)
{
char buf[100] = {0};
int ret;
while (ret = fread(buf,sizeof(char),sizeof(buf),fp_s))
fwrite(buf,sizeof(char),ret,fp_d);
}
//./a.out src dest
int main(int argc, const char *argv[])
{
if (argc != 3)
{
printf("Usage: %s <src> <dest>\n",argv[0]);
return -1;
}
FILE *fp_s = fopen(argv[1],"r");
FILE *fp_d = fopen(argv[2],"w");
if (fp_s == NULL || fp_d == NULL)
{
perror("fopen fail");
return -1;
}
//创建空洞文件
fseek(fp_s,0,SEEK_END);
long len = ftell(fp_s);
//1.偏移
fseek(fp_d,len-1,SEEK_SET);
//2.写
fputc('\0',fp_d);
fflush(fp_d);
// getchar();
rewind(fp_s);
rewind(fp_d);
do_copy(fp_s,fp_d);
fclose(fp_s);
fclose(fp_d);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//./a.out src dest
int main(int argc, const char *argv[])
{
if (argc != 3)
{
printf("Usage: %s <src> <dest>\n",argv[0]);
return -1;
}
FILE *fp_s = fopen(argv[1],"r");
FILE *fp_d = fopen(argv[2],"w");
if (fp_s == NULL || fp_d == NULL)
{
perror("fopen fail");
return -1;
}
//创建空洞文件
fseek(fp_s,0,SEEK_END);
long len = ftell(fp_s);
//1.偏移
fseek(fp_d,len-1,SEEK_SET);
//2.写
fputc('\0',fp_d);
fflush(fp_d);
// getchar();
rewind(fp_s);
rewind(fp_d);
// do_copy(fp_s,fp_d);
char *buf = malloc(len);
int ret = fread(buf,sizeof(char),len,fp_s);
fwrite(buf,sizeof(char),ret,fp_d);
fclose(fp_s);
fclose(fp_d);
return 0;
}
//1.打开
创建空洞文件
//2.拷贝
//3.关闭
2.实现插入功能,
指定要插入的位置 (如100个字节之后,)
指定要插入的内容 (如hello)
则最终效果是将 hell插入到了100个字节的后面
hello
a.定位到要插入的位置
后面的数据读走
b.重新定位到要插入的位置
输入数据
c.将刚才读走的数据写到插入数据的后面
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
if (argc!= 2)
{
printf("Usage: %s <file>\n",argv[0]);
return -1;
}
FILE *fp = fopen(argv[1],"r+");
if (fp == NULL)
{
perror("fopen fail");
return -1;
}
printf("Input a offset:");
int offset;
scanf("%d",&offset);
getchar();
printf("Input a string:");
char buf[100];
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
fseek(fp,0,SEEK_END);
long len = ftell(fp);
fseek(fp,offset,SEEK_SET);
char buff[len-offset];
int ret = fread(buff,sizeof(char),sizeof(buff),fp);
fseek(fp,offset,SEEK_SET);
fwrite(buf,sizeof(char),strlen(buf),fp);
fwrite(buff,sizeof(char),ret,fp);
fclose(fp);
return 0;
}
3.统计文本文件中a-z,出现的次数。
a:12
b:23
c:
z:34
#include<stdio.h>
int main(int argc, const char *argv[])
{
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int a[26] = {0};
char b;
while ((b = fgetc(fp)) != EOF)
{
if (b >= 'a' && b <= 'z')
{
a[b - 'a']++;
}
}
int i = 0;
for (i = 0; i < 26; ++i)
{
printf("%c:%d\n", 'a' + i, a[i]);
}
fclose(fp);
return 0;
}
4. 完成两个文件的合并操作。
./a.out 1.txt 2.txt 3.txt
#include<stdio.h>
int main(int argc, const char *argv[])
{
if (argc != 4) {
printf("Usage: %s <filename><filename><filename>\n", argv[0]);
return -1;
}
FILE *fp1 = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
FILE *fp3 = fopen(argv[3], "a+");
if (fp1 == NULL||fp2 == NULL ||fp3 == NULL) {
perror("Error opening file");
return -1;
}
char buf1[1024];
char buf2[1024];
int num1 = fread(buf1,1,sizeof(buf1),fp1);
fwrite(buf1,1,num1,fp3);
int num2 = fread(buf2,1,sizeof(buf2),fp2);
fseek(fp3,num1,SEEK_SET);
fwrite(buf2,1,num2,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}