By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,407 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,407 IT Pros & Developers. It's quick & easy.

read strings from file with values divided by ";"

P: n/a
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.

Jul 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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()
>istream& get (streambuf& sb, char delim );

extracts characters from the stream and inserts them in stream
buffer sb. Characters are extacted until either the delimiter
(parameter delim or '\n' if not speciffied) is found, or if the end
of file or any error occurs in the input or output sequences.
--
Bastien.
Jul 28 '06 #2

P: n/a
In article <11*********************@m79g2000cwm.googlegroups. com>,
"Denis Petronenko" <pe********@gmail.comwrote:
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<stringvec( 1 );
while ( getline( is, vec.back(), ';' )
vec.push_back("");
vec.pop_back();
Jul 28 '06 #3

P: n/a
A more C related response, that I find to make more senes, would be to
use the strtok function.
Daniel T. wrote:
In article <11*********************@m79g2000cwm.googlegroups. com>,
"Denis Petronenko" <pe********@gmail.comwrote:
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<stringvec( 1 );
while ( getline( is, vec.back(), ';' )
vec.push_back("");
vec.pop_back();
Jul 28 '06 #4

P: n/a
In article <11*********************@m79g2000cwm.googlegroups. com>,
pe********@gmail.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 28 '06 #5

P: n/a
as****@purdue.edu wrote:
Daniel T. wrote:
In article <11*********************@m79g2000cwm.googlegroups. com>,
"Denis Petronenko" <pe********@gmail.comwrote:
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<stringvec( 1 );
while ( getline( is, vec.back(), ';' )
vec.push_back("");
vec.pop_back();

A more C related response, that I find to make more senes, would be to
use the strtok function.
In accordance with custom here, please put your response below or
inline the post you are responding to.

A C++ way to do tokenizing can be found in the Boost.Tokenizer library
(http://boost.org/libs/tokenizer/index.html), and the OP might also
find the std::tr1::regex (aka Boost.Regex library,
http://boost.org/libs/regex/doc/index.html) helpful.

Cheers! --M

Jul 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.