473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3242
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************ *********@m79g2 000cwm.googlegr oups.com>,
"Denis Petronenko" <pe********@gma il.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<c harT, traits>&
getline(basic_i stream<charT, traits>& is,
basic_string<ch arT, 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<stringve c( 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************ *********@m79g2 000cwm.googlegr oups.com>,
"Denis Petronenko" <pe********@gma il.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<c harT, traits>&
getline(basic_i stream<charT, traits>& is,
basic_string<ch arT, 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<stringve c( 1 );
while ( getline( is, vec.back(), ';' )
vec.push_back(" ");
vec.pop_back();
Jul 28 '06 #4
In article <11************ *********@m79g2 000cwm.googlegr oups.com>,
pe********@gmai l.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_(deli miter) {}

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_iterato r 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("whateve r.txt");
myfile.imbue(st d::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.e du wrote:
Daniel T. wrote:
In article <11************ *********@m79g2 000cwm.googlegr oups.com>,
"Denis Petronenko" <pe********@gma il.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<c harT, traits>&
getline(basic_i stream<charT, traits>& is,
basic_string<ch arT, 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<stringve c( 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
3296
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 exactly the functionality I need, but I was thinking about trying to implement this in pure python. I did some reading on opening and reading binary files, etc., and was
10
2288
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 supposed to change the buffer size and see how this affects the read time. In other words, the buffer is supposed to limit how much of the file gets read per call, and cause some change in speed. I am supposed to do the same with fread as well, but...
3
1371
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 separate bytes() type."
7
911
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 how to use Command-Line Arguments inside C program.
2
5664
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" size="70" runat="Server"> <input type="text" SIZE="20" MAXLENGTH="20" id="Name" NAME="Name"> <input type="submit" id="Submit" runat="Server" value="Submit" OnServerClick="SubmitButton_Click"> </form>
3
9910
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 xmlns:bk="http://www.lucernepublishing.com"> <book> <title>Pride And Prejudice</title> </book>
6
3440
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 files. Example: produce x original, but not random strings from english alphabet, all lowercase. The length of each string and possible combinations is
9
2931
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 JavaScript. I have used a readymade javascript and there are variables like...
8
1899
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 to HTML. Question: Is a URL that starts with feed: output XML? Question: How do I open a feed: URL in PHP and parse it? Thanks!
1
14035
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(); ?> <input type="submit">
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.