473,398 Members | 2,212 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,398 software developers and data experts.

How to establish a delimiter pattern for input streams

I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample code? I am
attaching a "C" code snippet that does the same thing. The problem is
that I don't know how to realize this using "cin"
Thanks,
Lee

/****************************/
#include <stdio.h>

int
main()
{
int abc, xyz;

printf("Enter the values: ");

scanf("%d Step over this text %d", &abc, &xyz);

printf("VAR1: %d\n", abc);
printf("VAR2: %d\n", xyz);
}

Oct 30 '05 #1
7 4060
Generic Usenet Account wrote:
I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample code? I am
attaching a "C" code snippet that does the same thing. The problem is
that I don't know how to realize this using "cin"
Thanks,
Lee

/****************************/
#include <stdio.h>

int
main()
{
int abc, xyz;

printf("Enter the values: ");

scanf("%d Step over this text %d", &abc, &xyz);

printf("VAR1: %d\n", abc);
printf("VAR2: %d\n", xyz);
}


Here is one possibility:

--------------------------------------------------
#include <iostream>
#include <string>

int main(int, char **) {
std::string ignore;
int abc, xyz;

std::cout << "Enter the values: ";
std::cin >> abc >> ignore >> ignore >> ignore >> ignore >> xyz;

std::cout << "VAR1: " << abc << '\n';
std::cout << "VAR2: " << xyz << '\n';

return 0;
}

$ ./a.exe
Enter the values: 5 skip over this text 10
VAR1: 5
VAR2: 10
--------------------------------------------------

Though it requires you to know how many words you're skipping. There is
probably a better way. I might use a regex, but that would require some
system-dependent library which is not part of C++.

If you know how many words you have to skip, then this would probably
work fine.

--John Ratliff

P.S. Please don't cross-post. If you have a C++ question, post to
comp.lang.c++, if you have a D question, post to whatever newsgroup
handles the D language.

See http://www.parashift.com/c++-faq-lite/ for more information.
Oct 30 '05 #2
[cross-posting snipped]

On 30 Oct 2005 08:17:49 -0800, "Generic Usenet Account"
<us****@sta.samsung.com> wrote:
I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample code? I am
attaching a "C" code snippet that does the same thing. The problem is
that I don't know how to realize this using "cin"
Thanks,
Lee

/****************************/
#include <stdio.h>

int
main()
{
int abc, xyz;

printf("Enter the values: ");

scanf("%d Step over this text %d", &abc, &xyz);

printf("VAR1: %d\n", abc);
printf("VAR2: %d\n", xyz);
}


I would use the std::getline function which takes an istream &, a
string &, and optionally a delimiter character as arguments.

The first overload, taking just the first two arguments, reads an
entire line up to the new line character or EOF, whichever comes
first. The second overload will use the character you give it as a
delimiter. It is quite flexible as you don't have to know ahead of
time exactly how many elements between delimiters there might be ...
but 99% of the time you'll probably need to know, anyway.

One caveat ... I think the Borland 5 compiler (i.e. the RogueWave STL
libraries it uses) has troubles with this particular getline
function...if that's what you are using, you can fall back on
std::istream::getline which takes a buffer (i.e. char*) and a length
and an optional delimiter as arguments. Works just fine on my other
compilers, though...

--
Bob Hairgrove
No**********@Home.com
Oct 30 '05 #3
John Ratliff wrote:
I might use a regex, but that would require some
system-dependent library which is not part of C++.


You could use boost::regex. It is not system dependent, and it is included in the draft of
the next STL.

--

Valentin Samko - http://www.valentinsamko.com
Oct 30 '05 #4
John Ratliff wrote:

Here is one possibility:

--------------------------------------------------
#include <iostream>
#include <string>

int main(int, char **) {
std::string ignore;
int abc, xyz;

std::cout << "Enter the values: ";
std::cin >> abc >> ignore >> ignore >> ignore >> ignore >> xyz;

std::cout << "VAR1: " << abc << '\n';
std::cout << "VAR2: " << xyz << '\n';

return 0;
}


This is not quite the same thing. The suggested approach does not
distinguish between "Step over this text" and any other four-word
string. Is there no easy way to realize this using the streaming
library of C++?

Thanks,
Lee

Oct 31 '05 #5

us****@sta.samsung.com wrote:
John Ratliff wrote:

Here is one possibility:

--------------------------------------------------
#include <iostream>
#include <string>

int main(int, char **) {
std::string ignore;
int abc, xyz;

std::cout << "Enter the values: ";
std::cin >> abc >> ignore >> ignore >> ignore >> ignore >> xyz;

std::cout << "VAR1: " << abc << '\n';
std::cout << "VAR2: " << xyz << '\n';

return 0;
}


This is not quite the same thing. The suggested approach does not
distinguish between "Step over this text" and any other four-word
string. Is there no easy way to realize this using the streaming
library of C++?

Thanks,
Lee


As suggested, try Boost.RegEx.

http://boost.org/libs/regex/doc/index.html

Cheers! --M

Oct 31 '05 #6
On 2005-10-30, Generic Usenet Account <us****@sta.samsung.com> wrote:
I am trying to set up a delimiter character string for the
input stream such that the delimiter string is "skipped over"
in the input stream. Can someone suggest how to do this with
some sample code? I am attaching a "C" code snippet that does
the same thing. The problem is that I don't know how to
realize this using "cin"
Thanks,
Lee

/****************************/
#include <stdio.h>

int
main()
{
int abc, xyz;

printf("Enter the values: ");

scanf("%d Step over this text %d", &abc, &xyz);

printf("VAR1: %d\n", abc);
printf("VAR2: %d\n", xyz);
}


C++ doesn't come bundled with that functionality, that I know of.

Here's a quick implementation that more or less matches the scanf
functionality, and a tiny test program. If the ignored string is
not matched, it sets ios::failbit for the input stream.

#include <iostream>
#include <istream>
#include <cctype>
using namespace std;

class ignore
{
const char* s_;
public:
explicit ignore(const char* s): s_(s) { }
friend istream& operator>>(istream&, const ignore&);
};

istream& operator>>(istream& is, const ignore& ig)
{
is >> ws;
for (const char* p = ig.s_; *p; ++p) {
char ignore_c = *p;
if (isspace(static_cast<unsigned char>(ignore_c))) {
is >> ws;
} else {
char input_c = is.get();
if (input_c != ignore_c) {
is.setstate(ios::failbit);
return is;
}
}
}
return is;
}

int main()
{
int v1, v2;
cin >> v1 >> ignore("this text") >> v2;
if (cin) {
cout << "VAR1: " << v1 << "\nVAR2: " << v2;
} else {
cout << "Input error.\n";
}
return 0;
}

--
Neil Cerutti
Oct 31 '05 #7

Generic Usenet Account wrote:
I am trying to set up a delimiter character string for the input stream
such that the delimiter string is "skipped over" in the input stream.
Can someone suggest how to do this with some sample code? I am
attaching a "C" code snippet that does the same thing. The problem is
that I don't know how to realize this using "cin"
Thanks,
Lee


The correct way to handle this is to define an input stream manipulator
that accepts the "delimiter string" as an argument. This manipulator
should intelligently handle optional delimiter characters. If the
manipulator encounters the "delimiter string", it steps over it. If
not, it stores the value read into a "shared buffer", as described in
K&R Section 4.3. During reading, attempt is made to first read from
the "shared buffer" and then the input stream. Of course, this
requires changing the way the extraction operator works, which will not
happen unless this idea makes it to the standard.

We all know that manipulators that accept arguments are a little tricky
(The C++ Programming Language, Bjarne Stroustrup, 3rd Edition, Section
21.4.6.1) The way it is done is to define a class/structure that has
the overloaded operator >> friend function. This overloaded function
accepts the reference to the input stream and the reference to an
object of the aforementioned class/structure as arguments, and returns
the reference to the input stream. Here's an example:

struct skipDelimiter
{
const char* delimPattern;
skipDelimiter(const char* str): delimPattern(str){}
friend istream& operator>>(istream&, const skipDelimiter&);
};

istream& operator>>(istream& istrm, const skipDelimiter& sd)
{
// IF delimiter pattern found
// THEN
// skip over it
// ELSE
// push the pattern found into a shared buffer
// (as described in K&R Section 4.3)
// ENDIF
// return
return istrm;
}

For completeness, here's a code snippet for a manipulator that does not
take arguments. All that is needed here is a function that accepts the
reference to the input stream as an argument, and returns the reference
to the input stream.

istream& printdate(istream& istrm)
{
// Meaningless example, for illustrative
// purposes only
time_t lt;
time(&lt);
cout << ctime(&lt);
return istrm;
}
Regards,
Nimmi

Nov 2 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
1
by: Chris Coleman | last post by:
Hi, As some background, I have implemented a new stream object to work with tcp socket streams. I derive a new socket stream from basic_streambuf. I then derive a my sream object from my...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
4
by: Raquel | last post by:
Could someone explain to me what the reason is for having a character delimiter (which is double quotes by default) for performing Loads/Imports on UDB? I would think that column delimiter along...
3
by: magix | last post by:
Dear Guru, I have been thinking hard on how to token based on demiliter after certain position. Example, I have list of possible string below, and the the delimiter is "1" with the rules below...
0
by: tiggman | last post by:
Why preg_split works only if the char # before the delimiter is 21 or less? example php code: ============= $right = "{Hello Im very aaaaaa {cool|not cool}}"; print $right."<br>"; $pattern =...
4
by: Joel Barsotti | last post by:
I'm working on shipping rate calculator going back and forth via XML. The thing I'm confused about is that the code works, but after a few hours and I don't know how many requests, I start getting...
3
by: pankaj_wolfhunter | last post by:
Greetings, Some time back I got some good advice from this group. Again got stuck with something. I am using export utility to store data to flat files. For now I am using the following: ...
4
by: kretik | last post by:
I've been trying to coax this class to use something other than the default '$' but it seems setting it to something else has no discernible effect. Is it necessary to inherit from the class to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.