reading variables from a stream(beginner)

I

isaac2004

hello, what i am trying to do is set up a loop that grabs multiple
variables from an input file. my question is, is there a built in
function in the istream class that can grab multiple values on one
line.

such as

60 40 10 10

all getting put into different variables. the problem i am having is
that there is other data in the file that i do not want to do this to.
can i set the get for only the first line of data from the file.
thanks for the help
 
M

Mike Wahler

isaac2004 said:
hello, what i am trying to do is set up a loop that grabs multiple
variables from an input file. my question is, is there a built in
function in the istream class that can grab multiple values on one
line.

such as

60 40 10 10
all getting put into different variables. the problem i am having is
that there is other data in the file that i do not want to do this to.
can i set the get for only the first line of data from the file.
thanks for the help

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::vector<int> data;
std::ifstream file("myfile");

if(file)
{
std::string line;

if(std::getline(file, line))
{
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss),
std::istream_iterator<int>(),
std::back_inserter(data));
}

if(!file && !file.eof())
std::cerr << "Error reading from input file\n";
}
else
std::cerr << "Cannot open input file\n";

std::cout << "Data:\n";
std::copy(data.begin(), data.end(),
std::eek:stream_iterator<int>(std::cout, " "));
std::cout << '\n';
return 0;
}


Input file:

60 40 10 10
1 2 3
5 10 15


Output:

Data:
60 40 10 10


-Mike
 
J

James Kanze

hello, what i am trying to do is set up a loop that grabs multiple
variables from an input file. my question is, is there a built in
function in the istream class that can grab multiple values on one
line.
60 40 10 10
all getting put into different variables. the problem i am having is
that there is other data in the file that i do not want to do this to.
can i set the get for only the first line of data from the file.

Just chain the input:

std::cin >> a >> b >> c >> d ;

will read the four variables a, b, c, d.

If all variables are of the same type, you can copy using an
istream_iterator into a back_inserter into a container, but
you'll need to delimit the input in some way. If the delimiter
is the end of line, the easiest solution is to read the line
into a string, and then read the string using istringstream. If
the delimiter is some text character (e.g. a ';'), the easiest
solution is just to read using the istream_iterator---the
non-numeric character will cause an error, stopping the input,
after which, you can clear the error and continue reading other
things.
 
B

bluemarcin

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::vector<int> data;
std::ifstream file("myfile");

if(file)
{
std::string line;

if(std::getline(file, line))
{
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss),
std::istream_iterator<int>(),
std::back_inserter(data));
}

if(!file && !file.eof())
std::cerr << "Error reading from input file\n";
}
else
std::cerr << "Cannot open input file\n";

std::cout << "Data:\n";
std::copy(data.begin(), data.end(),
std::eek:stream_iterator<int>(std::cout, " "));
std::cout << '\n';
return 0;

}

Input file:

60 40 10 10
1 2 3
5 10 15

Output:

Data:
60 40 10 10

-Mike

andum
sim
 
J

Jim Langston

isaac2004 said:
hello, what i am trying to do is set up a loop that grabs multiple
variables from an input file. my question is, is there a built in
function in the istream class that can grab multiple values on one
line.

such as

60 40 10 10

all getting put into different variables. the problem i am having is
that there is other data in the file that i do not want to do this to.
can i set the get for only the first line of data from the file.
thanks for the help

It depends on what you are actualy trying to do. If you are trying to grab
an arbitrary number of values from a line (could be 4,could be 20,
whatever) what I like to do is read the entire line into a string, load it
into a stringstream then parse that. Something like this:

std::string Line;
std::getline( MyFile, Line );
std::stringstream Stream( Line );
int Value;
while( Stream >> Value )
{
// Do something with each value.
}

There are other ways, that is the way I normally do it though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top