C++ 更新:程序显示fstream的地址,而不是文本文件
我即将编写一个程序,询问用户是否要“搜索或转换”文件,如果他们选择C++ 更新:程序显示fstream的地址,而不是文本文件,c++,string,pointers,ifstream,C++,String,Pointers,Ifstream,我即将编写一个程序,询问用户是否要“搜索或转换”文件,如果他们选择convert,他们需要提供文件的位置 我不知道为什么程序显示文件的地址而不是打开它 以下是我的第一个方法: #include <fstream> #include <fstream> #include <iostream> #include <string> using namespace std; int main() { char dateiname[64], komman
convert
,他们需要提供文件的位置
我不知道为什么程序显示文件的地址而不是打开它
以下是我的第一个方法:
#include <fstream>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char dateiname[64], kommando[64];
ifstream iStream;
cout << "Choose an action: " << endl <<
" s - search " << endl <<
" c - convert" << endl <<
" * - end program" << endl;
cin.getline(kommando,64,'\n');
switch(kommando[0])
{
case 'c':
cout << "Enter a text file: " << endl;
cin.getline(dateiname,64,'\n');
iStream.open("C://users//silita//desktop//schwarz.txt");
case 's': break;
case '*': return 0;
default:
cout << "Invalid command: " << kommando << endl;
}
if (!iStream)
{
cout << "The file " << dateiname << " does not exist." << endl;
}
string s;
while (getline(iStream, s)) {
while(s.find("TIT", 0) < s.length())
s.replace(s.find("TIT", 0), s.length() - s.find("TIT", 3),"*245$a");
cout << iStream << endl;
}
iStream.close();
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
char dateiname[64],kommando[64];
ifstream;
cout首先,您不能使用==
比较c字符串。您必须使用strcmp(const char*,const char*)
。有关它的详细信息,您可以在那里找到:
例如:<代码>(i=“KovithErrEN”)< /C>必须成为<代码>如果(L.StcMP(i,Konvertieren))<< /Calp>< P> >在LASE的回答中提到,不能用C或C++来比较这种字符串;只是为了充实它,但是,我会解释为什么。< /P>
当您在您的问题中没有这样做时,如果我们使用C++字符串而不是字符数组,则我们将使用<代码> COMPARE()/<代码>方法来产生类似的影响:
#include <string>
using namespace std;
string MyString = "My C++ String"
if( MyString.compare("My C++ String")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}
#包括
使用名称空间std;
字符串MyString =“我的C++字符串”
如果(MyString)比较(“我的C++字符串”)=0){
//如果两个字符串之间的差异为0。。。
知道这些字符串在英语中的意思会有帮助吗?你能添加注释吗?字符串s打算用符号“500$a”替换所有“TIT”(表示用户输入的文本文件中的一个类别),所以这个程序是用来替换文本文件中的特定符号的。你解释了最明显的部分。我想我应该指定我的意思是你应该翻译字符串中的德语(?)单词,你cout
。有时人们可以猜(比如“Konverteren”听起来像“convert”),但这对某些人来说可能很难。有些短语,如“Nicht gefunden”是不可能猜到的。你完全正确,“konverteren”表示“转换”,“Suchen”表示搜索,“Date Nicht gefunden”表示文件不存在/找不到。这是我的实际代码(如上),问题仍然是程序在用户输入文本文件的地址之前就结束了。我的下一个问题是,我的程序在程序要求用户输入文本文件的地址之后就结束了。你知道我的错误是什么吗?谢谢
#include <cstring>
using namespace std;
char MyCharArr[] = "My Character Array"
if( strcmp(MyCharArr,"My Character Array")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}
#include <string>
using namespace std;
string MyString = "My C++ String"
if( MyString.compare("My C++ String")==0 ) {
// If there is 0 difference between the two strings...
cout << "This will now be run!" << endl;
}