2013-07-26 42 views
1

我在这个程序中遇到了一些麻烦,我无法弄清楚我做错了什么;该程序仍然不允许显示文件,它仍然不会使字母“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(); 
} 
+1

您是否打算修改原始文件或只是读取文件,修改内存中的文本,然后显示它?你的代码是否被编译? – nio

回答

2

为了编译代码(上gcc编译)我不得不做这些修改:

  1. 加入#include <cstdlib>用于待定义exit函数

  2. 改变行:

    xfile.open(nm);分成xfile.open(nm.c_str()); yfile.open(nm); into yfile.open(nm.c_str());

    因为nm是一个字符串,并且ifstream/ofstream.open需要普通的旧char数组。要将字符串转换为字符数组,您使用somestring.c_str()函数。

  3. 这个表达式valfile[valfile.length()-1]会返回valfile字符串中的最后一个字符...例如valfile [0]会返回一个来自文件的第一个字符,如果文件不为空。要纠正它只是打印出valfile

    cout << endl << '[' << valfile << ']' << endl; 
    
  4. 添加这种丑陋的黑客进入你的考试功能,在文件的开头大写可能a字符:

    void exam(string& valfile) 
    { 
        if((valfile.length()>=1) && (valfile[0]=='a')) valfile[0]='A'; 
        ... 
    
1

通行证char*ifstream构造,使用c_str()函数。

display()

yfile.open(nm.c_str());

nm()

xfile.open(nm.c_str());

添加#include <cstdlib>出境

刚刚拿到另一个想法:

#include <fstream> 
#include <string> 
#include<algorithm> 
#include<iterator> 
#include <sstream> 

using namespace std; 

int main() 

{ 
    vector <string> v; 

    ifstream ip("input.txt"); 
    ofstream op("output.txt"); 

    string str; 

    while (getline(ip, str)) 
      v.push_back(str); 

    transform(v.begin(), 
     v.end(), 
     ostream_iterator<string>(op,"\n"), 
     [](const string& x){ 
     stringstream iss(x); 
     vector <string> words; 
     string s; 
     //Split words 
     copy(istream_iterator<string>(iss), 
       istream_iterator<string>(), 
       back_inserter(words)); 
     for(auto& it:words) 
      if(it[0]=='a') 
       it[0]='A'; 
     s=words[0]; 
     for(auto it=1;it<words.size();++it) 
      s+=" "+words[it]; //Join words 
     return s; 
     } 
     ); 

    ip.close(); 
    op.close(); 
} 
//g++ -o test test.cpp -std=c++0x 
1

您可能想要从零开始迭代,您将跳过第一个字符。

void exam(string& valfile) 
{ 
    for(int i = 0; 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'; 
     } 
    } 
} 
1

从修改“考试”功能开始。循环写入的方式现在将访问字符串末尾的字符。

我改变主要为:

int main() 
{ 
    std::string s("a a a a a"); 

    std::cout << "before=[" << s << ']' << std::endl; 
    exam(s); 
    std::cout << " after=[" << s << ']' << std::endl; 

    return 0; 
} 

,并得到:

$ ./a.exe 
before=[a a a a a] 
after=[a A A A a] 

这是你想要的吗?

相关问题