如何逐行阅读文本留言并用逗号分隔文本 [英] How can I read a text lien by line and separate the text it by comma

查看:59
本文介绍了如何逐行阅读文本留言并用逗号分隔文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是C ++的新手。我有一个名为Read.txt的文件。它的用户名和密码如下。



username1,password1

username2,password2

用户名3,密码4





如何逐行使用C ++阅读本文,以后我该怎么办?单独的用户名和密码在单独的字符串中。



后来我需要在cstring类型的地图中传递用户名和密码。



问候,

Joy

Hello,

I am new to C++. I have a file named as "Read.txt".It has username and password as below.

username1, password1
username2, password2
username3, password4


How can I read this using C++ line by line and later how can I separate username and password in seperate string.

Later I need to pass username and password in map of cstring type.

Regards,
Joy

推荐答案

在那里你会找到你的解决方案:

http://www.cplusplus.com/forum/general/17771/ [ ^ ]
There you will find your solution:
http://www.cplusplus.com/forum/general/17771/[^]


#include <fstream>
#include <sstream>
#include <string>

int main()
{
    // Open file
    std::ifstream infile("D:\\accounts.txt");

    // read line by line
    std::string line;
    while (std::getline(infile, line))
    {
        std::istringstream iss(line);

        std::string username;
        std::string password;

        // read username and password from each line
        std::getline(iss, username, ',');
        std::getline(iss, password, ',');

        // use username/password further...
    }

	return 0;
}

</string></sstream></fstream>


这篇关于如何逐行阅读文本留言并用逗号分隔文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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