如何创建一个名为播放器用户名的文件? [英] How do I create a file with as name the players username?

查看:96
本文介绍了如何创建一个名为播放器用户名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试图创建一个测试登录程序。

(我知道这不安全但是我正在尝试创建一些要学习的东西。)

这一行给出了一个错误:file.open(用户名+。txt);

我认为+在那条线上不起作用,但我不确定。




这是错误:

c:\program files(x86)\ codeblocks \mingw \bin\..\lib\gcc \mingw32 \4.7.1 \ include \ c ++ \ stream | 702 |注意:void std :: basic_ofstream< _CharT,_Traits> :: open(const char *,std :: ios_base :: openmode)[with _CharT = char; _Traits = std :: char_traits< char> ;; std :: ios_base :: openmode = std :: _ Ios_Openmode] |





这是代码:

  #include   <   iostream  >  
#include < string >
#include < fstream >

使用 命名空间标准;

枚举 MainProgramParts {SUCCESS,FAILED,STOP,OPTIONS};
MainProgramParts选项();
MainProgramParts success();
MainProgramParts失败();

int main()
{
MainProgramParts nextPart = OPTIONS;

while (nextPart!= STOP){
switch ( nextPart){
case 选项:
nextPart = options();
break ;
case 成功:
nextPart = success();
break ;
case FAILED:
nextPart = failed();
break ;
}
}
}

MainProgramParts选项(){
int choice;

cout<< 1:注册<< ENDL;
cout<< 2:登录<< endl<< ENDL;
cin>>选择;

if (choice == 1
{
string username;
字符串密码;

cout<< 用户名:;
cin>>用户名;
cout<< 密码:;
cin>>密码;

ofstream文件;
file.open(用户名+ .txt);
file<<用户名<< endl<<密码;
file.close();

main();

if (choice == 2
{
string username;
字符串密码;
string un;
string pw;

cout<< 用户名:;
cin>>用户名;
cout<< 密码:;
cin>>密码;

ifstream read(用户名+ .txt);
getline(读取,取消);
getline(读,pw);

if (un == username&& pw == password)
{
返回成功;
}
else
{
return FAILED;
}
}
}
}

MainProgramParts success(){
cout<< 已成功登录!;
}

MainProgramParts失败(){
cout<< 密码/用户名不正确!;
}







谢谢,

~ Steff

解决方案

不,这不是连接,这是一个有效的 std :: string 运营商: http://www.cplusplus.com/reference/string/string/operator+ [< a href =http://www.cplusplus.com/reference/string/string/operator+target =_ blanktitle =New Window> ^ ]。



问题是类型,因为预期的参数类型是旧的lame char * 。使用 c_str 操作从 std :: string 实例中获取C字符串:http://www.cplusplus.com/reference/string/string/c_str [ ^ ]。



-SA


SA是正确的。



您可能会发现使用 visual-studio-community [ ^ ]。至少看一下。



你的代码可以正常工作,因为VSC符合C ++ 11标准。



在C ++ 11中允许将std :: string传递给open()。您可以简单地使用+运算符连接两个std :: strings来创建文件名。



 file.open(用户名+ < span class =code-string>  .txt); 





在其他编译器中,您必须传递char *(C字符串)。在连接两个字符串后,你可以使用c_str()从std :: string中获取一个C字符串。



 file.open ((filename +   .txt)。c_str()); 


Hello,

I tried to create a test login program.
(I know this isn't safe but I'm trying to create some things to learn).
This line gives an error: file.open(username + ".txt");
I think the + in that line doesn't work but I'm not sure.


This is the error:
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|


This is the code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

enum MainProgramParts{ SUCCESS, FAILED, STOP, OPTIONS };
MainProgramParts options();
MainProgramParts success();
MainProgramParts failed();

int main()
{
        MainProgramParts nextPart = OPTIONS;

        while(nextPart != STOP){
        switch(nextPart){
        case OPTIONS:
            nextPart = options();
            break;
        case SUCCESS:
            nextPart = success();
            break;
        case FAILED:
            nextPart = failed();
            break;
        }
    }
}

MainProgramParts options(){
    int choice;

    cout << "1: Register" << endl;
    cout << "2: Login" << endl << endl;
    cin >> choice;

    if(choice == 1)
    {
        string username;
        string password;

        cout << "Username: ";
        cin >> username;
        cout << "Password: ";
        cin >> password;

        ofstream file;
        file.open(username + ".txt");
        file << username << endl << password;
        file.close();

        main();

    if(choice == 2)
    {
    string username;
    string password;
    string un;
    string pw;

    cout << "Username: ";
    cin >> username;
    cout << "Password: ";
    cin >> password;

    ifstream read(username + ".txt");
    getline(read, un);
    getline(read, pw);

    if(un == username && pw == password)
    {
        return SUCCESS;
    }
    else
    {
        return FAILED;
    }
}
}
}

MainProgramParts success(){
    cout << "Successfully logged in!";
}

MainProgramParts failed(){
    cout << "Incorrect password / username!";
}




Thanks,
~Steff

解决方案

No, this is not the concatenation, which is a valid std::string operator: http://www.cplusplus.com/reference/string/string/operator+[^].

The problem is the type, as the expected argument type is old lame char*. Use the c_str operation to get a "C string" out of your std::string instance: http://www.cplusplus.com/reference/string/string/c_str[^].

—SA


SA is correct.

You may find it better to use visual-studio-community[^]. At least take a look at it.

Your code would then work fine because VSC is C++11 compliant.

Passing a std::string to open() is allowed in C++11. You may simply concatenate the two std::strings making your file name using the + operator.

file.open(username + ".txt");



In other compilers you will have to pass a char* (C string). You may use c_str() to get a C string from a std::string after concatenating the two strings.

file.open((filename + ".txt").c_str()); 


这篇关于如何创建一个名为播放器用户名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆