统计文本文件中的单词
题目
统计一个文本文件中的各个单词数量,假定
- 假定已经存在一个文本文件“words.txt”,其内容为多个单词,各单词之间以空格分割;
- 打开这个文件,搜索全部单词;
- 获取各个单词、个数以及单词总数量并显示出来;
算法分析
- 定义存放单词和个数的单词类;
- 定义单词类数组;
- 打开文件读;
- 循环读取一个单词,如果文件结束关闭文件;
- 保存单词及个数,并统计单词总数量;
- 关闭文件对象;
- 显示各单词和个数以及单词总数量;
实现
- 单词类
class wordtype
{
char word[20];
int count;
};
- 搜索单词模块程序代码
int getwords(wordtype *words){
ifstream in("words.txt"); // 打开英文文件
if(!in)
{
cout<<"文件打开错误!"<<endl;
return 1;
}
int n=0;
char word[20];
int m;
while(in)
{
in >> word; // 读单词
if(!in)
{
// 文件结尾时退出循环
break;
}
bool flag=false;
for(m=0; m<n; m++)
{
if(!strcmp(word,words[m].word))
{
// 已存在该单词
words[m].count++; // 原有单词计数加1
flag = true;
break;
}
}
if(!flag)
{
// 发现新单词
words[m].count = 1; // 新单词计数为1
strcpy(words[m].word,word); // 保存新单词
n++; // 总单词计数加1
}
}
in.close(); // 关闭文件
return n; // 返回单词个数
- 主函数测试
#include <iostream>
#include <fstream>
using namespace std;
int getwords(wordtype *words); // 单词搜索模块函数声明
int main()
{
wordtype words[100] = {"", 0}; // 单词结构体变量定义与初始化
int n = getwords(words);
cout<<"英文单词统计结果如下:"<<endl;
for(int m=0; m<n; m++)
{
cout<<words[m].word<<':'<<words[m].count<<endl;
}
cout<<"搜索出共"<<n<<"个单词。"<<endl;
return -1;
}
// int getwords <单词搜索模块函数定义在此!>
文本文件输入步骤小结
- 包含文件流类头文件:
#include<fstream>
- 打开文件读:
ifstream in("words.txt");
- 读文件:
in>>str;
- 用完关闭文件流:
in.close();
复制MP3二进制文件
题目
复制一个MP3二进制文件到另一个文件中
算法分析
- 输入原始文件和目标文件名;
- 分别按照读方式打开原始文件,按照写方式打开目标文件;
- 循环每次读取原始文件256字节,如果文件结尾则关闭原始文件和目标文件;
- 将实际读取的字节写入目标文件之中,转上一步 ;
- 关闭原始文件和目标文件;
二进制文件操作的方法
- 二进制文件打开方式为:
ios::binary
ofstream fout(szDestFile, ios::binary); // 写打开
ifstream fin(szOrigFile, ios::binary); // 读打开
- 定位文件开始:
fin.seekg(0L,ios::beg);
- 读文件内容:
fin.read(szBuf, sizeof(char) * 256)
- 实际读文件的内容长度:
int length=fin.gcount();
- 写文件内容:
fout.write(szBuf, length);
- 读写文件过程中错误判断:
if (fout.bad())
- 文件结尾判断:
while(!fin.eof())
- 关闭文件对象:
fout.close(); // 关闭目标文件
fin.close(); // 关闭原始文件
二进制文件复制模块代码
bool mp3cpy(const char * szDestFile, const char * szOrigFile)
{
ofstream fout(szDestFile, ios::binary); // 以二进制方式打开目标文件
ifstream fin(szOrigFile, ios::binary); // 以二进制方式打开原始文件
bool bRet = true;
if (fin.bad())
{
// 原始文件出错
bRet = false;
}
else
{
fin.seekg(0L,ios::beg); // 定位原始文件开始处
while(!fin.eof()){ // 原始文件未结尾
char szBuf[256] = {0};
fin.read(szBuf, sizeof(char) * 256); // 每次读取原始文件最多256字节
int length=fin.gcount(); // 实际读取的字节数
if (fout.bad())
{ // 目标文件出错
bRet = false;
break;
}
fout.write(szBuf, length); // 每次写入目标文件length个字节
}
}
fout.close(); // 关闭目标文件
fin.close(); // 关闭原始文件
return bRet;
主函数测试
#include <iostream>
#include <fstream>
using namespace std;
bool mp3cpy(const char * szDestFile, const char * szOrigFile);
// mp3文件复制函数声明
int main()
{
char szOrigFile[50]; // 原始文件名
char szDestFile[50]; // 目标文件名
cout<<"请输入原始文件名和目标文件名:";
cin>>szOrigFile;
cin>>szDestFile;
bool bRet=mp3cpy(szDestFile,szOrigFile); // 调用文件复制函数
if(bRet)
{
cout<<"文件复制成功!"<<endl;
}
else
{
cout<<"文件复制失败!"<<endl;
}
return -1;
}
// <mp3文件复制函数定义在此!>
二进制文件输入输出主要步骤小结
- 包含文件流类头文件:
#include<fstream>
- 打开文件读:
ifstream fin("a.mp3", ios::binary);
- 打开文件写:
ofstream fout("b.mp3", ios::binary);
- 读文件:
fin.read(szBuf, length);
- 写文件:
fout.write(szBuf, length);
- 用完关闭文件流:
fin.close();
fout.close();
学生成绩统计管理系统
题目
- 有四个学生信息文本文件,内容分别如下:
- 第一个文件是学号、姓名、班级等基本信息;
- 第二个文件是学号和高数成绩;
- 第三个文件是学号和大英成绩;
- 第四个文件是学号和计算机成绩。
- 请将这些文件按学号匹配合并为一个新的文本文件,新文件的每行内容如下:
- 学生学号、姓名、班级、高数成绩、大英成绩、计算机成绩和平均成绩
学生信息类
class Student {
// 学生信息类
public:
int no; // 学号
string name; // 姓名
string classname; // 班级
float math; // 高数成绩
float english; // 大英成绩
float computer; // 计算机成绩
float average; // 平均成绩
};
读取学生成绩文件函数
float getscore(string filename,int stdno)
{
int stdno_temp;
float score;
ifstream in(filename.c_str()); // 打开成绩文件
if( !in )
{ // 判断文件打开是否正确
cout<<"文件打开错误。"<<endl;
return 0;
}
bool flag=false;
while(in)
{
in>>stdno_temp>>score;
if(in)
{
// 正确时
if(stdno_temp==stdno)
{
flag=true;
break;
}
}
}
if(!flag)
{score=0;}
in.close();
return score;
}
学生信息写文件运算符重载函数
ostream & operator << (ostream & out, Student & student)
{
out<<student.no<<'\t'<<student.name <<'\t'<<student.classname;
out<<'\t'<<student.math;
out<<'\t'<<student.english;
out<<'\t'<<student.computer;
out<<'\t'<<student.average;
out<<endl;
}
算法分析
- 定义学号、姓名、班级、高数成绩、大英成绩、计算机成绩和平均成绩变量;
- 分别以读方式打开四个原始数据文件和以写方式打开一个目标文件;
- 循环从四个原始数据文件中依次读取学号、姓名、班级、高数成绩、大英成绩和计算机成绩,并计算平均成绩。读取出错时关闭文件;
- 将这些信息写入目标文件中,重复上一步骤;
- 关闭所有文件
主函数测试
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// 打开文件
ifstream in("info.txt"); // 学生信息文件
ofstream out("student.txt"); // 学生完整信息文件
if( !in || !out )
{
// 判断文件打开是否正确
cout<<"文件打开错误。"<<endl;
return 1;
}
out<<"学号\t\t姓名\t班级\t高数\t大英\t计算机\t平均"<<endl;
while(in)
{
Student student;
// 读文件
in>>student.no>>student.name>>student.classname;
if(!in)
{
// 读正确时才写文件
break;
}
student.math=getscore("math.txt",student.no);
student.english=getscore("english.txt",student.no);
student.computer=getscore("computer.txt",student.no);
student.average=(student.math+student.english+student.computer)/3;
// 写文件
out<<student;
}
//关闭文件
out.close();
in.close();
cout<<"student.txt文件建立成功,请查阅!"<<endl;
return 1;
}