read strings from file with values divided by ";"

D

Denis Petronenko

Hello,

how can i read into strings from ifstream?
file contains values in following format:
value11; val ue12; value 13;
valu e21;value22; value23;
etc.

i need to read like file >> string, but strings must be divided by ";"
separator.

Thanks.
 
B

Bastien Durel

Denis Petronenko a écrit :
Hello,

how can i read into strings from ifstream?
file contains values in following format:
value11; val ue12; value 13;
valu e21;value22; value23;
etc.

i need to read like file >> string, but strings must be divided by ";"
separator.

Thanks.
Hello,

You may play with istream::get()
 
D

Daniel T.

"Denis Petronenko said:
Hello,

how can i read into strings from ifstream?
file contains values in following format:
value11; val ue12; value 13;
valu e21;value22; value23;
etc.

i need to read like file >> string, but strings must be divided by ";"
separator.

Thanks.

template <class charT, class traits, class Alloc>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
basic_string<charT, traits, Alloc>& s,
charT delim);


Replaces the contents of s with characters read from the input
stream. It continues reading characters until it encounters the
character delim (in which case that character is extracted but not
stored in s), or until end of file. Note that getline, unlike
operator>>, does not skip whitespace.

So it's simply a matter of:

vector<string> vec( 1 );
while ( getline( is, vec.back(), ';' )
vec.push_back("");
vec.pop_back();
 
A

asoofi

A more C related response, that I find to make more senes, would be to
use the strtok function.
 
J

Jerry Coffin

Hello,

how can i read into strings from ifstream?
file contains values in following format:
value11; val ue12; value 13;
valu e21;value22; value23;
etc.

i need to read like file >> string, but strings must be divided by ";"
separator.

There are at least three distinct approaches to this. The one that's
most applicable will depend considerably on the basic idea of how
you're using the data.

There are three obvious possibilities. One is that there's one
specific part of your code that needs to read from a specified file
in this way. In this case, you probably want to use std::getline,
specifying ';' as the character that will terminate the 'line' that
it reads.

Another possibility is that a number of parts of your code need to
read from the file in this fashion. If this is the case, it's
probably best to centralize this "knowledge" about the file format
into one place, and use it everywhere else, typically by writing a
proxy class:

class delimited {
std::string data_;
char delimiter_;
public:
delimited(char delimiter) : delimiter_(delimiter) {}

operator std::string() { return data; }

friend std::istream &operator>>(std::istream &is, delimited &d)
{
return std::getline(is, d, d.delimiter);
}
};

This makes it a bit easier to read delimited strings from different
parts of your program without that code having to deal with the
(admittedly minimal) details of how to do so. This hiding does
accomplish a little bit though. For example, standard algorithms
operating on an istream_iterator automatically use operator>> to
extract data from the stream. This proxy allows them to do so
correctly.

A final possibility is that this is a characteristic of a type of
file, and it would be convenient for _all_ code that reads from this
kind of file to treat the semicolon as a separator, but ignore things
like spaces. In this case, you can create a ctype facet to reflect
that fact:

struct semictype: std::ctype<char>
{
semictype(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::ctype_base::mask* rc = 0;

if (rc == 0)
{
rc = new std::ctype_base::mask[
std::ctype<char>::table_size];
std::fill_n(rc, std::ctype<char>::table_size,
std::ctype_base::mask());
rc[';'] = std::ctype_base::space;
rc['\n'] = std::ctype_base::space;
}
return rc;
}
};

To use this, you create a stream, and then imbue the stream with a
locale using this facet:

std::ifstream myfile("whatever.txt");
myfile.imbue(std::locale(std::locale(), new semictype()));

And from then on, when you use operator>> with that file, it'll
extract semicolon-separated strings. Note that this ctype facet is
somewhat simplified though -- for example, since it doesn't classify
anything as a digit, attempting to extract a number from the file
won't work.
 
M

mlimber

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top