Click here to Skip to main content
15,894,042 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Posted
Comments
Afzaal Ahmad Zeeshan 31-Jul-15 5:08am    
Do you know there exists a function known as split, that splits the string based on a character?

There you will find your solution:
http://www.cplusplus.com/forum/general/17771/[^]
 
Share this answer
 
C++
#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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900