摘要:本文主要向大家介绍了C/C++知识点之2. C语言文件操作经典习题,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之2. C语言文件操作经典习题,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
1. 统计英文文本文件中,有多少个大写字母、小写字母、数字、空格、换行以及其他字符。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void main()
{
char path[100] = "c:\\统计.txt";
FILE *fp; //创建文件指针
fp = fopen(path, "r"); //打开文件,按照读的模式
if (fp == NULL)
{
printf("文件打开失败!\n");
return;
}
else
{
char ch;
int countBig = 0; //统计大写字母的个数
int countSmall = 0; //统计小写字母的个数
int contNum = 0; //统计数字的个数
int countEnter = 0; //统计换行的个数
int countSpace = 0; //统计空格的个数
int countOther = 0; //统计其他字符的个数
while ((ch = fgetc(fp)) != EOF) //获取一个字符,没有结束就继续
{
if (ch >= ‘A‘&&ch <= ‘Z‘) //判断是否大写字母
countBig++;
else if (ch >= ‘a‘&&ch <= ‘z‘) //判断是否小写字母
countSmall++;
else if (ch >= ‘0‘&&ch <= ‘9‘) //判断是否数字
contNum++;
else if (ch == ‘\n‘) //判断是否换行
countEnter++;
else if (ch == ‘ ‘) //判断是否空格
countSpace++;
else //其他字符
countOther++;
}
printf("大写字母:%d, 小写字母:%d, 数字:%d, 换行:%d, 空格:%d, 其他:%d\n", countBig, countSmall, contNum, countEnter, countSpace, countOther);
}
fclose(fp);
system("pause");
}
2. 编程实现搜索文件
Windows下:
遍历所有c盘下所有 *.txt *.exe dir.* :
for /r c:\ %i in (*.txt) do @echo %i
在c盘下搜索所有文件内容包含 hello 的文件:
for /r c:\ %a in (*) do @findstr /im "hello" "%a"
Linux下:(搜索时进入管理员权限)
指定目录搜索----确定文件名:
find /etc -name 1.c
搜索文件名中带c的文件:
find / -name *c*
从根目录开始查找所有扩展名为 .log 的文本文件,并找出包含“ERROR” 的行:
find / -type f -name "*.log" | xargs grep "ERROR"
Windows下代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void main()
{
/*按文件名进行搜索*/
//char path[100] = "C:\\Users\\Administrator\\Desktop"; //搜索用的目录
////搜索文件名用的通配符 *.txt表示所有文本文件
////1.*表示1开头的所有文件,*.*表示所有文件
//char fileName[30] = "*.txt";
//char outputPath[100] = "c:\\"; //保存输出结果的文件
//char cmd[512];
////for /r "%s" %i in (%s) do @echo %i >> "%s",path, fileName, outputPath
//sprintf(cmd, "for /r \"%s\" %%i in (%s) do @echo %%i >> \"%s\"", path, fileName, outputPath); //打印字符串
//system(cmd); //执行查找,并将结果输入到outputPath这个文件
//char showCmd[200];
//sprintf(showCmd, "type %s", outputPath);//打印字符串,便于查看
//system(showCmd); //显示
/*按文件内容进行搜索*/
char path[100] = "C:\\Users\\Administrator\\Desktop";
char str[20] = "hello";
char cmd[500];
char output[100] = "C:\\Users\\Administrator\\Desktop\\output.txt"; //保存输出结果的文件
//创建一个指令字符串,按照一段文本,在一个目录下检索所有包含了这段文本的文件
sprintf(cmd, "for /r %s %%a in (*) do @findstr /im \"%s\" \"%%a\" >> %s", path, str, output);
system(cmd); //执行指令
char showCmd[200];
sprintf(showCmd, "type %s", output);//打印字符串,便于查看
system(showCmd); //显示
system("pause");
}
Linux下代码如下:
#include
#include
void main()
{
/*按文件名进行搜索*/
//char str[20] = "*c*";
//char path[100] = "/home";
//char output[100] = "/home/output.txt";
//char cmd[100];
//sprintf(cmd, "find %s -name \‘%s\‘ >> %s", path, str,output);
//ystem(cmd);
//char show[100];
//sprintf(show, "cat %s", output);
//system(show);
/*按文件内容进行搜索*/
char path[100] = "/home";
char str[20] = "hello";
char findName[20] = "*.c";
char output[100] = "/home/output.txt";
char cmd[100];
char show[100];
sprintf(cmd, "find %s -type f -name \"%s\" | xargs grep %s >> %s", path, findName, str,output);
system(cmd);
sprintf(show, "cat %s", output);
system(show);
}
3. 统计文本文件中有多少个汉字。
GBK(汉字编码标准)规定,汉字,日文,韩文第一个字节大于128
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void main()
{
FILE *fp; //空的文件指针
char path[100] = "c:\\1.txt"; //文件路径
fp = fopen(path, "r"); //读的方式打开文件
if (fp == NULL)
perror("文件打开失败原因是:"); //输出错误原因
int ich; //获取字符,fgetc的返回值是int,不用int,ASCII码没有问题,但是汉字会出问题
//int占4个字节,用int才能存储汉字的编码,而char装不下
int countEng=0; //标记英文字符
int countNum=0; //标记数字
int countChin=0; //标记汉字
while ((ich = fgetc(fp)) != EOF)
{
if ((ich >= ‘A‘&&ich <= ich="">= ‘a‘&&ich <= ‘z‘))
countEng++; //字母自增
else if (ich >= ‘0‘&&ich <= ‘9‘)
countNum++; //数字自增
else if (ich > 128) //判断双字节字符
{
//此处仅得到了双字节,还需要增加判断汉字的代码(GBK编码规范)。。。此处省略
ich = fgetc(fp); //继续向前读一个字符
countChin++; //双字符自增
}
}
printf("英文字符%d, 数字%d, 双字节字符%d\n", countEng, countNum, countChin);
fclose(fp);
system("pause");
}
4. 文件加密和解密。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//1.txt加密后保存到1-Encrypt.txt;解密以后保存到1-Decrypt.txt
//加密最好按照二进制的方式加密,可以保证绝对精确
//文本的方式,换行符会被解析为/r/n,往往容易出现问题
//加密
void Encrypt(int passwd)
{
FILE *fpr; //读取1.txt
FILE *fpw; //写入1-Encrypt.txt
char pathr[100] = "c:\\1.txt";
char pathw[100] = "c:\\1-Encrypt.txt";
fpr = fopen(pathr, "r"); //读的模式打开需要加密的文件
fpw = fopen(pathw, "w"); //写的模式打开要写入的加密文件
if (fpr == NULL || fpw == NULL)
{
printf("文件故障,加密失败!\n");
return;
}
while (!feof(fpr)) //一直读到要加密的文件末尾
{
char ch = fgetc(fpr); //读取文本
/*字符移位加密*/
//ch = ch + passwd;
/*异或加密*/
ch = ch^passwd;
fputc(ch, fpw); //写入文件
}
fclose(fpr);
fclose(fpw);
}
//解密
void Decrypt(int passwd) //传入数组,传入长度
{
FILE *fpr; //读取1-Encrypt.txt
FILE *fpw; //写入1-Decrypt.txt
char pathr[100] = "c:\\1-Encrypt.txt";
char pathw[100] = "c:\\1-Decrypt.txt";
fpr = fopen(pathr, "r"); //读的模式打开加密后的文件1-Encrypt.txt
fpw = fopen(pathw, "w"); //写的模式打开解密后要写入的文件1-Decrypt.txt
if (fpr == NULL || fpw == NULL)
{
printf("文件故障,加密失败!\n");
return;
}
while (!feof(fpr)) //一直读到要解密的文件末尾
{
char ch = fgetc(fpr); //读取文本
/*字符移位解密*/
//ch = ch - passwd;
/*异或解密*/
ch = ch^passwd;
fputc(ch, fpw); //写入文件
}
fclose(fpr);
fclose(fpw);
}
void Encrypt_str(int passwd[], int len)
{
FILE *fpr; //读取1.txt
FILE *fpw; //写入1-Encrypt.txt
char pathr[100] = "c:\\1.txt";
char pathw[100] = "c:\\1-Encrypt.txt";
fpr = fopen(pathr, "rb"); //读的模式打开需要加密的文件 二进制加解密最精准
fpw = fopen(pathw, "wb"); //写的模式打开要写入的加密文件
if (fpr == NULL || fpw == NULL)
{
printf("文件故障,加密失败!\n");
return;
}
int i = 0; //标识取出加密数组的哪一位
while (!feof(fpr)) //一直读到要加密的文件末尾
{
char ch = fgetc(fpr); //读取文本
/*字符移位加密*/
//ch = ch + passwd;
/*异或加密*/
//ch = ch^passwd;
/*字符串移位加密*/
if (i > len - 1) //如果加密数组完成,就继续重新开始
i = 0;
ch = ch + passwd[i];
i++; //用完这个数组当前字符,移动到下一个字符
fputc(ch, fpw); //写入文件
}
fclose(fpr);
fclose(fpw);
}
void Decrypt_str(int passwd[], int len)
{
FILE *fpr; //读取1-Encrypt.txt
FILE *fpw; //写入1-Decrypt.txt
char pathr[100] = "c:\\1-Encrypt.txt";
char pathw[100] = "c:\\1-Decrypt.txt";
fpr = fopen(pathr, "rb"); //读的模式打开加密后的文件1-Encrypt.txt
fpw = fopen(pathw, "wb"); //写的模式打开解密后要写入的文件1-Decrypt.txt
if (fpr == NULL || fpw == NULL)
{
printf("文件故障,加密失败!\n");
return;
}
int i = 0;
while (!feof(fpr)) //一直读到要解密的文件末尾
{
char ch = fgetc(fpr); //读取文本
/*字符移位解密*/
//ch = ch - passwd;
/*异或解密*/
//ch = ch^passwd;
/*字符串移位解密*/
if (i > len - 1) //如果加密数组完成,就继续重新开始
i = 0;
ch = ch - passwd[i];
i++; //用完这个数组当前字符,移动到下一个字符
fputc(ch, fpw); //写入文件
}
fclose(fpr);
fclose(fpw);
}
void main()
{
/*字符移位加密*/
//Encrypt();
//Decrypt();
//Encrypt(6);
//Decrypt(6);
/*异或加密*/
//Encrypt(15);
//Decrypt(15);
/*字符串移位加密*/
char passwd[] = "helloworld";
int n = strlen(passwd);
Encrypt_str(passwd, n);
Decrypt_str(passwd, n);
system("pause");
}
5. 文件的分割与合并。
分割文件:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
//文件切割
void main()
{
char path[100] = "c:\\1-文件分割与合并"; //文件夹路径
char filename[50] = "test.mp4"; //文件名
char lastpath[150]; //存储文件的路径
sprintf(lastpath, "%s\\%s", path, filename);//构建文件的路径
int filesize = 0; //获取文件的大小
FILE *fpr = fopen(lastpath, "rb"); //二进制读入的方式打开
if (fpr == NULL)
{
perror("文件打开失败,原因是:");
return;
}
fseek(fpr, 0, SEEK_END); //文件指针移动到末尾
filesize = ftell(fpr); //获取文件指针与开头有几个字节
printf("文件有%d个字节\n", filesize);
int setsize = 1024 * 1024; //设置要分割的模块大小
int N; //存储一个文件最多可以分割为多少个模块
if (filesize%setsize == 0)
N = filesize / setsize;
else
N = filesize / setsize + 1;
fseek(fpr, 0, SEEK_SET); //文件指针移动到开头
char listpath[100] = "c:\\1-文件分割与合并\\list.txt";
FILE *fplist = fopen(listpath, "w"); //创建list.txt文件
for (int i = 1; i <= N; i++) //针对每一块进行处理
{
char temppath[120]; //存储每一块的路径
sprintf(temppath, "%s\\%d%s", path, i, filename);//构建每一块的文件名
printf("%s\n", temppath); //显示路径
fputs(temppath, fplist); //每一块的路径写入list.txt
fputc(‘\n‘, fplist); //写入换行符
FILE *fpb = fopen(temppath, "wb"); //按照二进制写入的模式打开文件
if (i < N)
{
for (int j = 0; j < setsize; j++) //前面N-1个都是完整的
{
int ich = fgetc(fpr); //读取一个字符
fputc(ich, fpb); //写入一个字符
}
}
else
{
for (int j = 0; j < filesize-(N-1)*setsize; j++) //最后一个不完整
{
int ich = fgetc(fpr); //读取一个字符
fputc(ich, fpb); //写入一个字符
}
}
fclose(fpb); //关闭写入的块文件
}
fclose(fplist); //关闭列表文件
fclose(fpr); //关闭读取的文件
system("pause");
}
文件合并:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//文件合并
void main()
{
char path[100] = "c:\\1-文件分割与合并\\list.txt"; //文件夹路径
FILE *fpr = fopen(path, "r"); //读的方式打开
char allpath[100] = "c:\\1-文件分割与合并\\test-all.mp4"; //合并后的文件
FILE *fpw = fopen(allpath, "wb"); //按照二进制写入的方式打开合并的文件
char temppath[200];
while (fgets(temppath, 200, fpr)) //不断循环地获取路径,fgets读到字符串值为非0;读不到或到末尾值为0
{
printf("%s", temppath); //字符串有换行符
int len = strlen(temppath); //取出字符串长度
temppath[len - 1] = ‘\0‘; //换行符替换为‘\0’
{
FILE *fpb = fopen(temppath, "rb"); //按照二进制读取的方式打开文件
int ich = fgetc(fpb); //读取一个字符
while (!feof(fpb)) //没有到文件结束就继续
{
fputc(ich, fpw); //写入要合并的文件
ich = fgetc(fpb);
}
fclose(fpb); //结束读取
}
}
fclose(fpw); //关闭要合并的文件的文件指针
fclose(fpr); //关闭list.txt的文件指针
system("pause");
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号