当密码有小写字母时,我想删除txt文件。
创建文件代码:
FILE *files[20];
char filename[20];
sprintf(filename, "%d.txt", i);
files[i] = fopen(filename, "w");
string login, pass;
//wprowadzanie zmian w licznie kont
fstream liczb;
liczb.open("liczbakont.txt", ios::out);
liczb<<i;
//tworzenie plików dla poszczególnych kont
fstream plik(to_string(i)+".txt");
plik<<"\n";
cout<<"Podaj login: "<<endl;
移除:
cout<<"pass: "<<endl;
cin>>pass;
if(islower (haslo[0]) )
{
if( remove(to_string(i)+".txt") == 0)
}
怎么了?
[错误] 无法将参数 '1' 的 'std::basic_string' 转换为 'const char*' 到 'int remove(const char*)' [错误] '}' 标记前的预期主表达式 [错误] '}' 标记前的预期声明
答案 0 :(得分:2)
看看你调用的函数的声明:
int remove( const char* fname );
要特别注意参数的类型。它是const char*
。但是,您用作参数的表达式 to_string(i)+".txt"
的类型不是 const char*
。类型为 std::string
。
您不能将一种类型的参数传递给需要另一种类型参数的函数 - 除非前一种类型可以隐式转换为后一种类型。 std::string
不能隐式转换为 const char*
。这是错误消息告诉您的内容:
[错误] 无法将参数 '1' 的 'std::basic_string' 转换为 'const char*' 到 'int remove(const char*)'
std::string
但是有一个返回 c_str
的成员函数 const char*
。现在查看 std::remove
的声明,您会发现它与参数的类型相匹配。因此,一个微不足道的修复是:
std::remove((to_string(i)+".txt").c_str())
更好的是,我建议改用 std::filesystem
:
std::filesystem::remove(to_string(i)+".txt")
<块引用>
[错误] '}' 标记前的预期主表达式 [错误] '}' 标记前的预期声明
此错误表明您的 if 语句格式错误。示例:
// wrong
{
if(condition)
}
// correct
{
if(condition)
statement;
}
答案 1 :(得分:0)
c_str()
从 const char*
获取 std::string
。if
语句之后没有语句,因此它调用了第二个错误。添加一些语句以执行或删除额外的 if
语句。添加一些语句(在本例中为空语句 ;
):
if(islower (haslo[0]) )
{
if( remove((to_string(i)+".txt").c_str()) == 0);
}
删除多余的 if
:
if(islower (haslo[0]) )
{
remove((to_string(i)+".txt").c_str()) == 0;
}