更新:程序显示 fstream 的地址而不是文本文件
Update: program shows adress of fstream instead of the text file
我要写一个程序,询问用户是否要"search or convert"一个文件,如果他们选择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();
}
起初您无法使用 ==
比较 C 字符串。您必须使用 strcmp(const char*, const char*)
。您可以在此处找到有关它的更多信息:http://www.cplusplus.com/reference/cstring/strcmp/
例如:if (i == "Konvertieren")
必须变成 if(!strcmp(i,"Konvertieren"))
正如 Lassie 的回答中提到的,您不能使用 c 或 c++ 以这种方式比较字符串;然而,为了充实它,我将解释 为什么 。
char MyCharArr[] = "My Character Array"
// MyCharArr is now a pointer to MyCharArr[0],
// meaning it's a memory address, which will vary per run
// but we'll assume to be 0x00325dafa
if( MyCharArr == "My Character Array" ) {
cout << "This will never be run" << endl;
}
这里 if 将一个指针 (MyCharArr) 与一个字符数组文字进行比较,该指针将是一个内存地址,即一个整数。明明是0x00325dafa != "My Character Array".
使用 cstrings(字符数组),您需要使用 cstring
库中的 strcmp()
函数,它会给您一个数字,告诉您 "how different" strings 本质上是给差异一个数值。在本例中我们只对 no difference 感兴趣,即 0,所以我们需要的是:
#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;
}
虽然您在问题中没有这样做,但如果我们使用的是 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;
}
我要写一个程序,询问用户是否要"search or convert"一个文件,如果他们选择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();
}
起初您无法使用 ==
比较 C 字符串。您必须使用 strcmp(const char*, const char*)
。您可以在此处找到有关它的更多信息:http://www.cplusplus.com/reference/cstring/strcmp/
例如:if (i == "Konvertieren")
必须变成 if(!strcmp(i,"Konvertieren"))
正如 Lassie 的回答中提到的,您不能使用 c 或 c++ 以这种方式比较字符串;然而,为了充实它,我将解释 为什么 。
char MyCharArr[] = "My Character Array"
// MyCharArr is now a pointer to MyCharArr[0],
// meaning it's a memory address, which will vary per run
// but we'll assume to be 0x00325dafa
if( MyCharArr == "My Character Array" ) {
cout << "This will never be run" << endl;
}
这里 if 将一个指针 (MyCharArr) 与一个字符数组文字进行比较,该指针将是一个内存地址,即一个整数。明明是0x00325dafa != "My Character Array".
使用 cstrings(字符数组),您需要使用 cstring
库中的 strcmp()
函数,它会给您一个数字,告诉您 "how different" strings 本质上是给差异一个数值。在本例中我们只对 no difference 感兴趣,即 0,所以我们需要的是:
#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;
}
虽然您在问题中没有这样做,但如果我们使用的是 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;
}