C++ 使用ifstream从字符串读取数据的特定部分

C++ 使用ifstream从字符串读取数据的特定部分,c++,fstream,C++,Fstream,我编写了一个程序,基本上从主项目文件中保存的文本文件中读取2行。值得注意的是,我的操作系统是Windows。我只需要阅读第一行和第二行的特定部分。例如,我有一个文本文件,它有两行:User:Administrator和password:stefan。在我的程序中,我要求用户输入用户名和密码,并检查它是否与文本文件中的用户名和密码匹配,但是行中包含一些不必要的字符串:“user:”和“password:”。除了不必要的字母外,还有什么方法可以阅读所有内容吗?这是我用来读取文件的代码: #inclu

我编写了一个程序,基本上从主项目文件中保存的文本文件中读取2行。值得注意的是,我的操作系统是Windows。我只需要阅读第一行和第二行的特定部分。例如,我有一个文本文件,它有两行:User:Administrator和password:stefan。在我的程序中,我要求用户输入用户名和密码,并检查它是否与文本文件中的用户名和密码匹配,但是行中包含一些不必要的字符串:“user:”和“password:”。除了不必要的字母外,还有什么方法可以阅读所有内容吗?这是我用来读取文件的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream myfile("Hello.txt");
    string str, str2;
    getline (myfile, str);
    getline(myfile, str2);
    return 0;
} 
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream myfile(“Hello.txt”);
字符串str,str2;
getline(myfile,str);
getline(myfile,str2);
返回0;
} 

其中str是文本文件的第一行,str2是第二行。

此代码从名为
user.txt的文件加载用户和密码

文件内容:

user john_doe
password disneyland
它使用
getline(myfile,line)
读取一行,使用
istringstream-iss(line)
并将用户和密码存储在单独的字符串中

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main()
{

    string  s_userName;
    string  s_password ;
    string line,temp;

    ifstream myfile("c:\\user.txt");

    // read line from file
    getline( myfile, line );


    // split string and store user in  s_username
    istringstream iss(line);
    iss >> temp;
    iss >> s_userName;

    // read line from file
    getline( myfile, line );

    // split string and store password in  s_password
    istringstream iss2(line);
    iss2 >> temp;
    iss2 >> s_password;

    //display
    cout << "User     : " << s_userName << " \n"; 
    cout << "Password : " << s_password << " \n";
    cout << " \n";

    myfile.close();
    return 0;
} 
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串s_用户名;
字符串s_密码;
字符串行,临时;
ifstream myfile(“c:\\user.txt”);
//从文件中读取行
getline(myfile,line);
//拆分字符串并将用户存储在s_用户名中
istringstream iss(线);
国际空间站>>温度;
iss>>s_用户名;
//从文件中读取行
getline(myfile,line);
//拆分字符串并将密码存储在s_password中
istringstream iss2(线);
iss2>>温度;
iss2>>s_密码;
//展示

cout此代码从名为
user.txt的文件加载用户和密码

文件内容:

user john_doe
password disneyland
它使用
getline(myfile,line)
读取一行,使用
istringstream-iss(line)
并将用户和密码存储在单独的字符串中

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main()
{

    string  s_userName;
    string  s_password ;
    string line,temp;

    ifstream myfile("c:\\user.txt");

    // read line from file
    getline( myfile, line );


    // split string and store user in  s_username
    istringstream iss(line);
    iss >> temp;
    iss >> s_userName;

    // read line from file
    getline( myfile, line );

    // split string and store password in  s_password
    istringstream iss2(line);
    iss2 >> temp;
    iss2 >> s_password;

    //display
    cout << "User     : " << s_userName << " \n"; 
    cout << "Password : " << s_password << " \n";
    cout << " \n";

    myfile.close();
    return 0;
} 
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串s_用户名;
字符串s_密码;
字符串行,临时;
ifstream myfile(“c:\\user.txt”);
//从文件中读取行
getline(myfile,line);
//拆分字符串并将用户存储在s_用户名中
istringstream iss(线);
国际空间站>>温度;
iss>>s_用户名;
//从文件中读取行
getline(myfile,line);
//拆分字符串并将密码存储在s_password中
istringstream iss2(线);
iss2>>温度;
iss2>>s_密码;
//展示

我想下面的答案会对你有帮助:我刚刚检查了它,但我的编译器不受支持。有没有更简单的方法?如果没有,我就更改我的编译器?我想下面的答案会对你有帮助:我刚检查了它,但我的编译器不受支持。有没有更简单的方法?如果没有,我就更改我的编译器