我在这个程序中遇到了一些麻烦,我无法弄清楚我做错了什么;该程序仍然不允许显示文件,它仍然不会使字母“a”大写。该程序应该从外部文件input.txt中读取,将所有以字母“a”开头的单词大写,然后将它们写入外部文件output.txt。 任何帮助将不胜感激!谢谢!输入/输出文件(大写的一个字母)
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void nm(string nm, ifstream& xfile);
void mov(ifstream& xfile, string& valfile);
void exam(string& valfile);
void display(string nm, ofstream& yfile, string& valfile);
int main()
{
ifstream xfile;
ofstream yfile;
string valfile;
nm("input.txt", xfile);
mov(xfile, valfile);
exam(valfile);
display("output.txt", yfile, valfile);
}
void nm(string nm, ifstream& xfile)
{
xfile.open(nm);
if (xfile.fail())
{
cerr << "Unable to open file \"" << nm << "\" for reading.\n";
exit(1);
}
}
void mov(ifstream& xcode, string& valfile)
{
while (xcode.good())
{
valfile += xcode.get();
}
xcode.close();
cout << endl << '[' << valfile[valfile.length()-1] << ']' << endl;
valfile = valfile.substr(0, valfile.length()-1);
}
void exam(string& valfile)
{
for(int i = 1; i < valfile.length(); i++)
{
if( valfile[i] == 'a' && isspace((int) valfile[i-1]) &&
(isspace((int) valfile[i+1]) || isalpha((int) valfile[i+1])) )
{
valfile[i] = 'A';
}
}
}
void display(string nm, ofstream& yfile, string& valfile)
{
yfile.open(nm);
yfile << valfile;
yfile.close();
}
您是否打算修改原始文件或只是读取文件,修改内存中的文本,然后显示它?你的代码是否被编译? – nio