现在的位置: 首页 > 综合 > 正文

C++ 学习笔记9

2012年12月22日 ⁄ 综合 ⁄ 共 939字 ⁄ 字号 评论关闭

 

Read a text file and write it to a target text file, changing all lowercase to uppercase and double-spacing the output text.

 

读取一个文本文件并将其写入目标文件,目标文件中将所有的小写字母转换成大写字母,并以双倍行距输出文本。

 

name.txt

fanzhaoxin
BUSH Will
Sadamu
Micro jaoden

 

//本程序在VCSP6下编译通过,用户创建一个name.txt的文件,输出写入一个name2.txt的文件中
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;

void double_space(ifstream& f, ofstream& t)
{
 char  c;
 char  d;
 
 while (f.get(c))
 {  
  if(c>='a'&&c<='z')
  {  
   d=c-32;
      t.put(d);//大小写替换
  }
  if (c == '\n')
  { t.put(c);
   t.put(c);//遇到回车,输出两个回车,即实现双倍行距
  }
  if(c<97&&c!='\n'||c>122&&c!='\n')
   t.put(c);
  
 }
}

int main()
{

 ifstream f_in;
 ofstream f_out;
 
   f_in.open("name.txt",ios::in); //读入一个存名字的文件,txt格式
   f_out.open("name2.txt",ios::out);//输出一个存名字的文件,双倍行距,小写替换成大写
 

 
 if (!f_in)
 {
  cout << "cannot open "  << endl;
  exit(1);//can not open return wrong
 }
 if (!f_out)
 {
  cout << "cannot open "  << endl;
  exit(1);//the same as f_in
 }
 double_space(f_in, f_out);//use the function to achieve your goal
 return 0;
}

 

抱歉!评论已关闭.