Jump to content

C++ read from .txt file and assign to string

Zuccers
Go to solution Solved by databreach,

You should be able to do so using:

#include <sstream>
#include <string>

std::string line;
while (std::getline(infile, line))
{
    std::istringstream iss(line);
    int a, b;
    if (!(iss >> a >> b)) { break; } // error

    // process pair (a,b)
}

 

More solutions here: https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c

Hello all, I have a small problem with C++, I am trying to read from a .txt file and line by line assign to string or integer, I can't figure anything out on this part, so I don't have any code, tried searching, but couldn't find and the w3 schools example with "getline()" doesn't work. Any help would be very useful.
Gratefully,
Zuccers.

Link to comment
Share on other sites

Link to post
Share on other sites

You should be able to do so using:

#include <sstream>
#include <string>

std::string line;
while (std::getline(infile, line))
{
    std::istringstream iss(line);
    int a, b;
    if (!(iss >> a >> b)) { break; } // error

    // process pair (a,b)
}

 

More solutions here: https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Zuccers said:

Hello all, I have a small problem with C++, I am trying to read from a .txt file and line by line assign to string or integer, I can't figure anything out on this part, so I don't have any code, tried searching, but couldn't find and the w3 schools example with "getline()" doesn't work. Any help would be very useful.
Gratefully,
Zuccers.

You already have your solution but I highly suggest reading through this website https://en.cppreference.com/w/cpp/header as it lists all the C++ STL (standard template library) libraries which are all under the std namespace. It also includes descriptions and functional code examples for each function and library. Highly useful for novices and experts a like because it has everything under the sun.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, trag1c said:

You already have your solution but I highly suggest reading through this website https://en.cppreference.com/w/cpp/header as it lists all the C++ STL (standard template library) libraries which are all under the std namespace. It also includes descriptions and functional code examples for each function and library. Highly useful for novices and experts a like because it has everything under the sun.

Thanks, will do, it will come in handy.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×