事先用WIndows的记事本建立一个文本文件ff.txt

1.编写一个函数void ReadFile(char*s)实现读取以s串为文件名的文本文件的内容并在屏幕上显示
2.编写一个函数void change(char*s1,char*s2)将文本文件的小写字母全部改写成大写字母并生成一个新文件ff2.txt
3.主函数中调用的ReadFile(ff.txt)显示ff.txt的内容,调用 change(ff.txt,ff2.txt),根据ff.txt文件作修改,生成一个新的文件ff2.txt,组后ReadFile(ff2.txt),显示新文件的内容

第1个回答  2020-05-30
#include <fstream>
#include <iostream>
#include<string>
using namespace std;
void ReadFile(const char *s);
void Change(const char *s1, const char *s2);
int main()
{
ReadFile("F:\\ff.txt");
Change("F:\\ff.txt", "F:\\ff2.txt");
ReadFile("F:\\ff2.txt");
return 0;
}
void ReadFile(const char *s)
{
char ch;
ifstream inf(s);
if (!inf)
{
cout << "Cannot open the file\n";
return;
}
while (inf.get(ch))
cout << ch;
cout << endl;
inf.close();
}
void Change(const char *s1, const char *s2)
{
ifstream ifile("F:\\ff.txt");
if (!ifile)
{
cout << "ff.txt cannot open" << endl;
return;
}
ofstream ofile("F:\\ff2.txt");
if (!ofile)
{
cout << "ff2.txt cannot open" << endl;
return;
}
char ch;
while (ifile.get(ch))
{
if (ch >= 'a'&&ch <= 'z')
ch -= 32;
ofile.put(ch);
}
ifile.close();
ofile.close();
}