确定是C++? 如果是下面是一个简单实现的例子。 定义了一个函数,输入和输出文件可根据需要自行设定。
#include
#include
#include
using namespace std;
int CaseConvert(string infile, string outfile);
struct CaseReverse {
int operator()(const int &c) {
if (islower(c))
return toupper(c);
if (isupper(c))
return tolower(c);
else
return c;
}
};
int main(int argc, char** argv)
{
string infile("in.txt");
string outfile("out.txt");
int retval;
retval = CaseConvert(infile, outfile);
if (!retval)
cout << "Done!" << endl;
else
cout << "Failed. error code " << retval << endl;
return 0;
}
int CaseConvert(string infile, string outfile)
{
ifstream ifs(infile.c_str());
ofstream ofs(outfile.c_str());
string buffer;
int retcode = 0;
if (!ifs)
return -1;
if (!ofs)
return -2;
while (::getline(ifs, buffer))
{
transform(buffer.begin(), buffer.end(), buffer.begin(),
CaseReverse() );
ofs << buffer << endl;
}
if (!ifs.eof())
retcode = -3;
ifs.close();
ofs.close();
return retcode;
}