输入/输出文件(一个字母的大写)
Input/ output file (Capitalization of one letter)
我在这个程序上遇到了一些问题,我不知道我做错了什么;该程序仍然不允许显示文件,并且仍然不会大写字母"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编译器上),我必须进行以下修改:
-
为待定义的
exit
函数添加了#include <cstdlib>
-
更改的行:
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()
函数。 -
此表达式
valfile[valfile.length()-1]
将返回valfile字符串中的最后一个字符。。。例如,若文件不是空的,valfile[0]将只返回文件中的一个(First)字符。要更正它,只需打印出valfile
:cout << endl << '[' << valfile << ']' << endl;
-
将这个丑陋的破解添加到你的考试函数中,在文件开头大写可能的
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
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 我从int x[3]得到的一个非常奇怪的输出;
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 我的输出中有一个额外的 0,为什么会这样
- 以C++输出一个数字三角形
- 当使用我在一个函数、另一个函数中修改的数组时,在我的输出中得到一个奇怪的负数
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- C++:为什么最后一个输出不是地址以及如何获取值 9
- 如何仅使用循环/控制语句和两个cout语句创建一个倒三角:一个输出“#”和一个输出' n'
- 如何制作一个输出流,将未指定的类型括在C++的括号内
- C++我在运行时创建一个输出文件,如何再次读取此输出文件作为输入
- 我不明白这段代码的最后一个输出
- 将两个正则表达式结果连接到一个输出字段中,一次只能使用一个
- 为什么我得到一个输出"b(1) dc dvfunc"?
- 矩阵从一个输入文件到LAPACK,再反向到一个输出文件
- 从文件流创建一个输出文件