1. 统计英文文本文件中,有多少个大写字母、小写字母、数字、空格、换行以及其他字符。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> void main()
{
char path[] = "c:\\统计.txt";
FILE *fp; //创建文件指针
fp = fopen(path, "r"); //打开文件,按照读的模式 if (fp == NULL)
{
printf("文件打开失败!\n");
return;
}
else
{
char ch;
int countBig = ; //统计大写字母的个数
int countSmall = ; //统计小写字母的个数
int contNum = ; //统计数字的个数
int countEnter = ; //统计换行的个数
int countSpace = ; //统计空格的个数
int countOther = ; //统计其他字符的个数 while ((ch = fgetc(fp)) != EOF) //获取一个字符,没有结束就继续
{
if (ch >= 'A'&&ch <= 'Z') //判断是否大写字母
countBig++;
else if (ch >= 'a'&&ch <= 'z') //判断是否小写字母
countSmall++;
else if (ch >= ''&&ch <= '') //判断是否数字
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 <stdio.h>
#include <stdlib.h> 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[] = "C:\\Users\\Administrator\\Desktop";
char str[] = "hello";
char cmd[];
char output[] = "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[];
sprintf(showCmd, "type %s", output);//打印字符串,便于查看
system(showCmd); //显示 system("pause");
}

  Linux下代码如下:

#include <stdio.h>
#include <stdlib.h> 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[] = "/home";
char str[] = "hello";
char findName[] = "*.c";
char output[] = "/home/output.txt";
char cmd[];
char show[];
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 <stdio.h>
#include <stdlib.h> void main()
{
FILE *fp; //空的文件指针
char path[] = "c:\\1.txt"; //文件路径
fp = fopen(path, "r"); //读的方式打开文件 if (fp == NULL)
perror("文件打开失败原因是:"); //输出错误原因 int ich; //获取字符,fgetc的返回值是int,不用int,ASCII码没有问题,但是汉字会出问题
//int占4个字节,用int才能存储汉字的编码,而char装不下
int countEng=; //标记英文字符
int countNum=; //标记数字
int countChin=; //标记汉字 while ((ich = fgetc(fp)) != EOF)
{
if ((ich >= 'A'&&ich <= 'Z') || (ich >= 'a'&&ich <= 'z'))
countEng++; //字母自增
else if (ich >= ''&&ich <= '')
countNum++; //数字自增
else if (ich > ) //判断双字节字符
{
//此处仅得到了双字节,还需要增加判断汉字的代码(GBK编码规范)。。。此处省略
ich = fgetc(fp); //继续向前读一个字符
countChin++; //双字符自增
}
} printf("英文字符%d, 数字%d, 双字节字符%d\n", countEng, countNum, countChin); fclose(fp);
system("pause");
}

4. 文件加密和解密。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //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[] = "c:\\1.txt";
char pathw[] = "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[] = "c:\\1-Encrypt.txt";
char pathw[] = "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[] = "c:\\1.txt";
char pathw[] = "c:\\1-Encrypt.txt"; fpr = fopen(pathr, "rb"); //读的模式打开需要加密的文件 二进制加解密最精准
fpw = fopen(pathw, "wb"); //写的模式打开要写入的加密文件 if (fpr == NULL || fpw == NULL)
{
printf("文件故障,加密失败!\n");
return;
} int i = ; //标识取出加密数组的哪一位
while (!feof(fpr)) //一直读到要加密的文件末尾
{
char ch = fgetc(fpr); //读取文本 /*字符移位加密*/
//ch = ch + passwd; /*异或加密*/
//ch = ch^passwd; /*字符串移位加密*/
if (i > len - ) //如果加密数组完成,就继续重新开始
i = ;
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[] = "c:\\1-Encrypt.txt";
char pathw[] = "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 = ;
while (!feof(fpr)) //一直读到要解密的文件末尾
{
char ch = fgetc(fpr); //读取文本 /*字符移位解密*/
//ch = ch - passwd; /*异或解密*/
//ch = ch^passwd; /*字符串移位解密*/
if (i > len - ) //如果加密数组完成,就继续重新开始
i = ;
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 <stdio.h>
#include <stdlib.h> //文件切割
void main()
{
char path[] = "c:\\1-文件分割与合并"; //文件夹路径
char filename[] = "test.mp4"; //文件名
char lastpath[]; //存储文件的路径
sprintf(lastpath, "%s\\%s", path, filename);//构建文件的路径 int filesize = ; //获取文件的大小
FILE *fpr = fopen(lastpath, "rb"); //二进制读入的方式打开 if (fpr == NULL)
{
perror("文件打开失败,原因是:");
return;
} fseek(fpr, , SEEK_END); //文件指针移动到末尾
filesize = ftell(fpr); //获取文件指针与开头有几个字节
printf("文件有%d个字节\n", filesize); int setsize = * ; //设置要分割的模块大小
int N; //存储一个文件最多可以分割为多少个模块
if (filesize%setsize == )
N = filesize / setsize;
else
N = filesize / setsize + ; fseek(fpr, , SEEK_SET); //文件指针移动到开头
char listpath[] = "c:\\1-文件分割与合并\\list.txt";
FILE *fplist = fopen(listpath, "w"); //创建list.txt文件 for (int i = ; i <= N; i++) //针对每一块进行处理
{
char temppath[]; //存储每一块的路径
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 = ; j < setsize; j++) //前面N-1个都是完整的
{
int ich = fgetc(fpr); //读取一个字符
fputc(ich, fpb); //写入一个字符
}
}
else
{
for (int j = ; j < filesize-(N-)*setsize; j++) //最后一个不完整
{
int ich = fgetc(fpr); //读取一个字符
fputc(ich, fpb); //写入一个字符
}
}
fclose(fpb); //关闭写入的块文件
} fclose(fplist); //关闭列表文件
fclose(fpr); //关闭读取的文件
system("pause");
}

  文件合并:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //文件合并
void main()
{
char path[] = "c:\\1-文件分割与合并\\list.txt"; //文件夹路径
FILE *fpr = fopen(path, "r"); //读的方式打开
char allpath[] = "c:\\1-文件分割与合并\\test-all.mp4"; //合并后的文件
FILE *fpw = fopen(allpath, "wb"); //按照二进制写入的方式打开合并的文件
char temppath[];
while (fgets(temppath, , fpr)) //不断循环地获取路径,fgets读到字符串值为非0;读不到或到末尾值为0
{
printf("%s", temppath); //字符串有换行符
int len = strlen(temppath); //取出字符串长度
temppath[len - ] = '\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");
}

2. C语言文件操作经典习题的更多相关文章

  1. C语言文件操作 FILE结构体

    内存中的数据都是暂时的,当程序结束时,它们都将丢失.为了永久性的保存大量的数据,C语言提供了对文件的操作. 1.文件和流 C将每个文件简单地作为顺序字节流(如下图).每个文件用文件结束符结束,或者在特 ...

  2. go语言文件操作,这期资料比较详细( 欢迎加入go语言群: 218160862 )

    go语言文件操作,这期资料比较详细 欢迎加入go语言群: go语言深圳群 golang深圳 218160862 点击加入 文件操作 func Open(name string) (file *File ...

  3. C语言文件操作

    C语言文件操作,以下以基本的例子和说明来展开怎么通过C语言来进行文件操作. 操作文件,我们得需要知道什么?当然是路径和文件名. 首先我需要知道我操作的文件在哪里,叫什么名字.在C语言中还存在一个打开方 ...

  4. C 语言文件操作

    C 语言文件操作 1. 数据流:     程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流.     所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...

  5. C语言文件操作函数

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...

  6. C语言文件操作解析(五)之EOF解析(转载)

      C语言文件操作解析(五)之EOF解析 在C语言中,有个符号大家都应该很熟悉,那就是EOF(End of File),即文件结束符.但是很多时候对这个理解并不是很清楚,导致在写代码的时候经常出错,特 ...

  7. 【转】C语言文件操作解析(三)

    原文网址:http://www.cnblogs.com/dolphin0520/archive/2011/10/07/2200454.html C语言文件操作解析(三) 在前面已经讨论了文件打开操作, ...

  8. C语言文件操作函数大全(超详细)

    C语言文件操作函数大全(超详细) 作者: 字体:[增加 减小] 类型:转载 本篇文章是对C语言中的文件操作函数进行了详细的总结分析,需要的朋友参考下   fopen(打开文件)相关函数 open,fc ...

  9. C语言文件操作相关函数

    在实际应用中,我们往往需要对文件进行操作,下面我将介绍C语言的一些关于操作文件的函数. 一.计算机文件 计算机文件是以计算机硬盘为载体存储在计算机上的信息集合,是存储在某种长期储存设备上的一段数据流. ...

随机推荐

  1. 微信小程序中在页面中实现下拉刷新显示提醒语后在消失

    最近在做小程序的时候遇见一个问题,就是页面要下拉刷新给客户一个提醒语,查看了小程序的官方文档 这里有个注意点:如果你是一页进行下拉刷新就在那个文件夹的json里面加上"enablePullD ...

  2. Sprite Editor

    [Sprite Editor] 在Unity3D中,一个图片可以有多种类型(如下图).对于2D游戏开发,最常用的类型就是Sprite. 下图是Sprite Texture的属性,Packing Tag ...

  3. java Web jsp和servlet的关系

    JSP在本质上就是SERVLET,但是两者的创建方式不一样Servlet完全是JAVA程序代码构成,擅长于流程控制和事务处理,通过Servlet来生成动态网页很不直观JSP由HTML代码和JSP标签构 ...

  4. IIS设置Access-Control-Allow-Origin

    打开iis,找到“HTTP响应标头”点进去, 在右侧可以看到添加,然后添加如下标头即可Access-Control-Allow-Headers:Content-Type, api_key, Autho ...

  5. 【codevs2822】爱在心中

    题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度 ...

  6. C++——STL之vector, list, deque容器对比与常用函数

    STL 三种顺序容器的特性对比: vector 可变数组,内存空间是连续的,容量不会进行缩减.支持高效随机存取,即支持[]和at()操作.尾部插入删除效率高,其他位置插删效率较低: list 双向链表 ...

  7. SHELL读取 ini 格式文件做配置文件

    ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...

  8. MCMC 破译密码 http://mlwhiz.com/blog/2015/08/21/MCMC_Algorithms_Cryptography/

    # AIM: To Decrypt a text using MCMC approach. i.e. find decryption key which we will call cipher fro ...

  9. linux下mariadb的下载与卸载

    Linux下mariadb的安装 使用阿里云的mariadb yum install mariadb-server mariadb -y 启动mariadb数据库 systemctl start/st ...

  10. brk/sbrk和mmap行为分析程序

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> // #include <malloc. ...