473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

read strings from file with values divided by ";"

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
5 3206
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: cjl | last post by:
Hey all: I am working on a little script that needs to pull the strings out of a binary file, and then manipulate them with python. The command line utility "strings" (part of binutils) has...
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
3
by: Petr Prikryl | last post by:
Hi all, My question is: How do you tackle with mixing Unicode and non-Unicode parts of your application? Context: ======== The PEP 3000 says "Make all strings be Unicode, and have a...
7
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
2
by: IkBenHet | last post by:
Hello, I am uploading a file using this form in ASP.NET. I have also added a simpel textfield: <form runat="server" enctype="multipart/form-data"> <input type="file" id="oFile" Name="oFile"...
3
by: ano | last post by:
Hi, Anyone knows how to get "xmlns" value from XML file? For example, how to check that this xml file has a xmlns or not? Or how to read the xmlns value? <bookstore...
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
9
by: Mahernoz | last post by:
Hello Friends, The JavaScript File exmplmenu_var.js contains the code... (for the sake of brevity i am showing only that code which needs to be changed) I am actually developing a menu using...
8
by: eastcoastguyz | last post by:
I'm very new to RSS. I was given a URL that starts with "feed:". I want to be able to open this and read in its content into a PHP program to produce a report. I'm not interested in converting it...
1
by: ChollaPete | last post by:
This code: <form action="processScan.php" method="get"> <p> <?php print "Scan name: <input type=\"file\" name=\"tScanFileName\" value= \"{$scanFileName}\"><br>"; addHiddenCarryons(); ?>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.