输入/输出文件(一个字母的大写)

Input/ output file (Capitalization of one letter)

本文关键字:一个 输出 文件 输入      更新时间:2023-10-16

我在这个程序上遇到了一些问题,我不知道我做错了什么;该程序仍然不允许显示文件,并且仍然不会大写字母"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();
}

为了编译你的代码(在gcc编译器上),我必须进行以下修改:

  1. 为待定义的exit函数添加了#include <cstdlib>

  2. 更改的行:

    xfile.open(nm);转换为xfile.open(nm.c_str());yfile.open(nm);转化为yfile.open(nm.c_str());

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

  3. 此表达式valfile[valfile.length()-1]将返回valfile字符串中的最后一个字符。。。例如,若文件不是空的,valfile[0]将只返回文件中的一个(First)字符。要更正它,只需打印出valfile:

    cout << endl << '[' << valfile << ']' << endl;
    
  4. 将这个丑陋的破解添加到你的考试函数中,在文件开头大写可能的a字符:

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

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

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';
        }
    }
}

从纠正"检查"函数开始。按照现在编写循环的方式,它将访问字符串末尾之后的字符。

我把主要内容改为:

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]

这是你想要的吗?

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