473,652 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

several input-output questions

Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));

However, I also tried to define istream_iterato r separately. I did

istream_iterato r<stringit(in) ;

this works, but there's no way I can define something equivalent to
istream_iterato r<string>() above. I tried

istream_iterato r<stringit;

and this failed. What does "istream_iterat or<string>()" mean? Does
it define an iterator that's not bound to any istream? If so, why my
code above "istream_iterat or<stringit;" doesn't work?

There are some error-detection functions, such as "bad(), "good()",
"fail()". I'm not sure what constitutes "errors". If I read from a
file and hit the end of the file, does it leave the stream in an error
state? If the reading reaches EOF, then do I need to use "clear()" to
reset it? If I use "clear()" to reset a stream (for whatever error
reasons), are the remaining data on the stream lost?

Thanks a lot!
Jess

Jun 17 '07 #1
10 1679
Jess wrote:
Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));

However, I also tried to define istream_iterato r separately. I did

istream_iterato r<stringit(in) ;

this works, but there's no way I can define something equivalent to
istream_iterato r<string>() above. I tried

istream_iterato r<stringit;

and this failed.
It should work (apart from the fact that you seem to have used the
variable name 'it' twice). What error did you get?

What does "istream_iterat or<string>()" mean? Does
it define an iterator that's not bound to any istream? If so, why my
code above "istream_iterat or<stringit;" doesn't work?

There are some error-detection functions, such as "bad(), "good()",
"fail()". I'm not sure what constitutes "errors". If I read from a
file and hit the end of the file, does it leave the stream in an error
state?
Yes

If the reading reaches EOF, then do I need to use "clear()" to
reset it?
Yes, but be careful here, reaching the end of file is not the same as
trying to read past the end of file. Only the latter is an error.

If I use "clear()" to reset a stream (for whatever error
reasons), are the remaining data on the stream lost?
No
>
Thanks a lot!
Jess
Jun 17 '07 #2
On Jun 17, 11:16 pm, John Harrison <john_androni.. .@hotmail.com>
wrote:
Jess wrote:
Hello,
When I copy contents from an input string to a vector, I typically use
something like this
vector<stringv;
ifstream in("file");
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));
However, I also tried to define istream_iterato r separately. I did
istream_iterato r<stringit(in) ;
this works, but there's no way I can define something equivalent to
istream_iterato r<string>() above. I tried
istream_iterato r<stringit;
and this failed.

It should work (apart from the fact that you seem to have used the
variable name 'it' twice). What error did you get?
Thanks, I tried it again, and this error disappears. :) By the way,
it seems both "istream_iterat or it();" and "istream_iterat or it;"
work, is there any difference?
Jess

Jun 17 '07 #3
Jess wrote:
On Jun 17, 11:16 pm, John Harrison <john_androni.. .@hotmail.com>
wrote:
>Jess wrote:
Hello,
When I copy contents from an input string to a vector, I typically use
something like this
vector<stringv;
ifstream in("file");
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));
However, I also tried to define istream_iterato r separately. I did
istream_iterato r<stringit(in) ;
this works, but there's no way I can define something equivalent to
istream_iterato r<string>() above. I tried
istream_iterato r<stringit;
and this failed.

It should work (apart from the fact that you seem to have used the
variable name 'it' twice). What error did you get?

Thanks, I tried it again, and this error disappears. :) By the way,
it seems both "istream_iterat or it();" and "istream_iterat or it;"
work, is there any difference?
istream_iterato r it();

declares it as a function returning an istream_iterato r, whereas

istream_iterato r it;

defines it as an istream_iterato r object and calls its default constructor.

--
rbh
Jun 17 '07 #4
Jess wrote:
On Jun 17, 11:16 pm, John Harrison <john_androni.. .@hotmail.com>
wrote:
>Jess wrote:
>>Hello,
When I copy contents from an input string to a vector, I typically use
something like this
vector<string v;
ifstream in("file");
copy(istream_ iterator<string (in), istream_iterato r<string>(),
back_inserter (v));
However, I also tried to define istream_iterato r separately. I did
istream_itera tor<stringit(in );
this works, but there's no way I can define something equivalent to
istream_itera tor<string>() above. I tried
istream_itera tor<stringit;
and this failed.
It should work (apart from the fact that you seem to have used the
variable name 'it' twice). What error did you get?

Thanks, I tried it again, and this error disappears. :) By the way,
it seems both "istream_iterat or it();" and "istream_iterat or it;"
work, is there any difference?
Jess
Well this time I will say that it should NOT work. This

istream_iterato r<stringit;

declares a variable called 'it' of type istream_iterato r<string>, no
problem. But this

istream_iterato r<stringit();

declares a FUNCTION called 'it' which takes zero arguments and returns a
istream_iterato r<string>. This is a common newbie mistake, by adding the
brackets you've written a function prototype.

I guess this was the source of your original confusion.

john
Jun 17 '07 #5
I see my problems now, thanks!

On the other hand, in program

copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));

Does it convert "in" to an istream_iterato r<string>? If so, what is
"istream_iterat or<string>()"? Neither of them looks like a function
call or constructing an istream_iterato r<stringobject. ..

Jess

Jun 17 '07 #6
On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
I see my problems now, thanks!

On the other hand, in program

copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));

Does it convert "in" to an istream_iterato r<string>? If so, what is
"istream_iterat or<string>()"? Neither of them looks like a function
call or constructing an istream_iterato r<stringobject. ..

Jess
'istream_iterat or<string>(in)' creates a temporary object based on 'in'
'istream_iterat or<string>()' creates a temporary object representing the
end iterator.
'back_inserter( v)' creates a temporary object

Consider:

class T {};

T t = T();

--
Obnoxious User
Jun 17 '07 #7
On Jun 17, 11:44 pm, Obnoxious User <O...@127.0.0.1 wrote:
On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
I see my problems now, thanks!
On the other hand, in program
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));
Does it convert "in" to an istream_iterato r<string>? If so, what is
"istream_iterat or<string>()"? Neither of them looks like a function
call or constructing an istream_iterato r<stringobject. ..
Jess

'istream_iterat or<string>(in)' creates a temporary object based on 'in'
'istream_iterat or<string>()' creates a temporary object representing the
end iterator.
'back_inserter( v)' creates a temporary object
Consider:

class T {};

T t = T();
I'm sometimes confused by the object creation syntax. To create an
object t of type T, I think I can use the following statements.

T t; //default constructor
T t(arg); //constructor with argument arg
T t = T(); //call default constructor for T()? then call copy
constructor to create t using the temporary object created from T()
T t = T(arg); //does the right hand side call the constructor with
argument arg, then call the copy constructor to get t?
T t = existing_t_obje ct;
T* tp = new T; //call default constructor
T* tp = new T(arg); //constructor with argument arg

Are they correct? Have I missed something?

Thanks,
Jess

Jun 17 '07 #8
On Jun 17, 3:52 pm, Jess <w...@hotmail.c omwrote:
I see my problems now, thanks!
On the other hand, in program
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));
Does it convert "in" to an istream_iterato r<string>? If so, what is
"istream_iterat or<string>()"? Neither of them looks like a function
call or constructing an istream_iterato r<stringobject. ..
According to the standard, they are both function style casts.
The first converting in into an istream_iterato r<string>, and
the second converting nothing into an istream_iterato r<string>.
In practice, most people view them as explicit creation of a
temporary, the first initialized with "in", and the second
initialized using the default constructor. (Converting nothing
into something doesn't sound much like a cast in most people's
mind, regardless of what the standard calls it.)

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 17 '07 #9
On Jun 17, 4:25 pm, Jess <w...@hotmail.c omwrote:
On Jun 17, 11:44 pm, Obnoxious User <O...@127.0.0.1 wrote:
On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
I see my problems now, thanks!
On the other hand, in program
copy(istream_it erator<string(i n), istream_iterato r<string>(),
back_inserter(v ));
Does it convert "in" to an istream_iterato r<string>? If so, what is
"istream_iterat or<string>()"? Neither of them looks like a function
call or constructing an istream_iterato r<stringobject. ..
'istream_iterat or<string>(in)' creates a temporary object based on 'in'
'istream_iterat or<string>()' creates a temporary object representing the
end iterator.
'back_inserter( v)' creates a temporary object
Consider:
class T {};
T t = T();
I'm sometimes confused by the object creation syntax. To create an
object t of type T, I think I can use the following statements.
T t; //default constructor
T t(arg); //constructor with argument arg
T t = T(); //call default constructor for T()? then call copy
constructor to create t using the temporary object created from T()
T t = T(arg); //does the right hand side call the constructor with
argument arg, then call the copy constructor to get t?
T t = existing_t_obje ct;
T* tp = new T; //call default constructor
T* tp = new T(arg); //constructor with argument arg
Are they correct? Have I missed something?
They're all correct for creating named objects. There are,
however, two types unnamed objects: those created using operator
new (dynamically allocated objects), and temporary objects. A
temporary object is the result of an expression: in the case of
an object of class type, a function call or a "cast". Thus, for
example: "static_cas t< T >( arg )" behaves exactly like "T
t(arg)", except that the resulting object is unnamed. Of
course, there are two other ways of writing
"static_cast<T> (arg)": "(T)arg" and "T(arg)". In the latter
case, the standard also allows 0 or more than one argument; it
still calls it a cast, even if it doesn't seem very logical.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 17 '07 #10

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

Similar topics

8
4019
by: John Banta | last post by:
Hi, I have created a 12 month calendar where each day has a check box whereby the user can indicate if that day is available or not available for a certain event. The calendar is 'drawn' in a single form rather than 12 separate forms. If the checkbox contained in each day within each month has a unique name such as 1August2003, 2August2003, etc, is there a way in Javascript where I could have a button by each month where the user...
17
16159
by: wolfing1 | last post by:
Like, I know if several checkboxes have the same name, I can get the ones that were checked after submit with a request.form("name") and maybe do a split(request.form("name"),",") to cycle through them. I tried the same thing with textfields (input type=text), but doesn't seem to work the same. Do they behave differently?
6
6141
by: Iver Erling Årva | last post by:
I am looking for a way to let the users copy e.g. a multi-line address from a textfile and paste it into a webpage where there is one input field for each address line in such a way that not only the first line is pasted, but instead the program automatically jumps to the next field and put line 2 in there and so on. Can this be done? I tried with onkeydown to check for event.keyCode == 13, but it doesn't seem to work with paste. Brgds
6
2413
by: wolfing1 | last post by:
how would I go doing it without javascript and using 'POST'? Having a page with a variable list of items in a shopcart, each item with its own 'remove' button. How could I do this without a javascript and using POST as the form type? (so having <a href="page.htm?remove=2> is not possible)
5
2715
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or more documents to share/use during a meeting presentation. What would be the most efficient way to approach this? This is the logic I'm currently considering: Page 1: Meeting Information input with link to a document upload page (this page...
3
2913
by: alice | last post by:
I've been trying for a long time to figure this out, to have a page with several MP3 clips, and each one having a custom start and stop button next to them to play the track. I finally found a bit of code that does this, but it's made for only one mp3 file. I don't know enough about javascript to customize the code myself. I know there probably needs to be an "array" of some sort, but I need some help figuring out where and how that works....
2
2881
by: Otto Wyss | last post by:
In a form I've several buttons which submits the form and I'd like to do different checks in the onsubmit event handler. How can I find out which button was activated? <script type="text/javascript"> function checkInput (t) { if (t. ...) return false; return true; } ....
4
2384
by: TP | last post by:
Hi everybody, When using raw_input(), the input of the user ends when he types Return on his keyboard. How can I change this behavior, so that another action is needed to stop the input? For example, CTRL-G. It would allow the user to input several lines. Thanks Julien
2
1189
by: metafizzical | last post by:
Hi, I have an object that contains the main data for my script. For example: function Invoice() { this.name="bob" this.email="abc@example.com" } I'm trying to access and edit the object properties through several different functions - However, I don't seem to know the Right Way to do this ;)
0
8370
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
8811
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
8590
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
7302
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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
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
4147
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
2707
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
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.