將檔案中內容進行大小寫轉化

來源:互聯網
上載者:User

#include<iostream>
#include<stdio.h>
#include<vector>
#include<fstream>
#include<string>
#include<ctype.h>
using namespace std;

class FileNotOpen //檔案無法開啟異常類
{
public:
 string what()
 {
  string str="檔案無法開啟,沒有此檔案或目錄!";
  return str;
 }
};
class Convert //轉換類
{
private:
 void ToCapital(string& str); //將字串轉變為大寫
 void ToLowcase(string& str); //將字串轉變為小寫
 void ReadFile(const string& filename,vector<string>& tmp);//將檔案內容讀入到向量tmp中
 void WriteFile(const string& filename,vector<string>& tmp); ////將向量tmp內容寫入檔案中

public:
 void LowcaseToCapitalOfFile(const string& filename); //將檔案中小寫字母轉變為大寫字母
 void CapitalToLowcaseOfFile(const string& filename); //將檔案中大寫字母轉變為小寫字母
 double StringToDouble(const string& str); //將字串轉變為雙精確度浮點數
 //可以增加其他許多轉換方法
};

void Convert::ToCapital(string& str)
{
 string::iterator it;
 for(it=str.begin();it!=str.end();it++)
 {
  *it=toupper(*it);
 }
}

void Convert::ToLowcase(string& str)
{
 string::iterator it;
 for(it=str.begin();it!=str.end();it++)
 {
  *it=tolower(*it);
 }
}

void Convert::ReadFile(const string& filename,vector<string>& tmp)
{
 ifstream infile; //定義輸入資料流檔案
 try
 {
  infile.open(filename.c_str(),ios::in); //以讀方式開啟此檔案
  if(!infile) throw FileNotOpen();
 }
 catch(FileNotOpen& e)
 {
  cerr<<e.what()<<endl;
  exit(-1);
 }
 catch(...)
 {
  cerr<<"Something unexpected!"<<endl;
 }
 string s;
 while(getline(infile,s)) tmp.push_back(s); //將檔案內容讀入到向量tmp中
 infile.clear(); //清除檔案流
 infile.close(); //關閉檔案
}

void Convert::WriteFile(const string& filename,vector<string>& tmp)
{
 ofstream outfile; //定義輸出檔案流
 try 
 {
  outfile.open(filename.c_str(),ios::out); //以寫方式開啟此檔案
  if(!outfile) throw FileNotOpen();
 }
 catch(FileNotOpen& e)
 {
  cerr<<e.what()<<endl;
  exit(-1);
 }
 catch(...)
 {
  cerr<<"Something unexpected!"<<endl;
 }
 vector<string>::iterator it;
 for(it=tmp.begin();it!=tmp.end();it++) outfile<<(*it)<<endl; 
 outfile.clear();
 outfile.close();
}

void Convert::LowcaseToCapitalOfFile(const string &filename)
{
 vector<string> tmp;
 this->ReadFile(filename,tmp);
 vector<string>::iterator it;
 for(it=tmp.begin();it!=tmp.end();it++)
 {
  this->ToCapital(*it);
 }
 this->WriteFile(filename,tmp);
}

void Convert::CapitalToLowcaseOfFile(const string &filename)
{
 vector<string> tmp;
 this->ReadFile(filename,tmp);
 vector<string>::iterator it;
 for(it=tmp.begin();it!=tmp.end();it++)
 {
  this->ToLowcase(*it);
 }
 this->WriteFile(filename,tmp);
}

void main()
{
 string filename;
 cout<<"請輸入檔案名稱:";
 getline(cin,filename);
 Convert c;
 //c.CapitalToLowcaseOfFile(filename);
 c.LowcaseToCapitalOfFile(filename);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.