Connecting Tech Pros Worldwide Forums | Help | Site Map

How should I specify size?

William Payne
Guest
 
Posts: n/a
#1: Jul 22 '05
Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted to C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

// William Payne



Mike Wahler
Guest
 
Posts: n/a
#2: Jul 22 '05

re: How should I specify size?



"William Payne" <mikas493_no_s_p_a_m_@student.liu.se> wrote in message
news:bvm97i$443$1@news.island.liu.se...[color=blue]
> Hello, when using cin.getline() with a char array, how should I specify[/color]
the[color=blue]
> size (the second parameter of getline)?
>
> const int buffer_size = 256;
> char buffer[buffer_size];
>
> std::cin.getline(buffer, std::streamsize(buffer_size));
> or
> std::cin.getline(buffer, buffer_size);
> ?
>
> I know, I should use std::string and I usually do, but I still want to[/color]
know[color=blue]
> the proper way. I am writing a program that will be probably converted to[/color]
C[color=blue]
> so I ended up with char arrays instead of std::string, and the program[/color]
uses[color=blue]
> a third-party api which is based on C.[/color]

The C++ standard shows these prototypes:

basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);

so I'd use 'streamsize' type.

But with C, you don't have that type, use 'size_t' to specify
object sizes or counts. And when calling library functions,
simply use whatever type the prototype specifies.


-Mike


William Payne
Guest
 
Posts: n/a
#3: Jul 22 '05

re: How should I specify size?



"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:_0yTb.9596$uM2.7429@newsread1.news.pas.earthl ink.net...[color=blue]
>
> "William Payne" <mikas493_no_s_p_a_m_@student.liu.se> wrote in message
> news:bvm97i$443$1@news.island.liu.se...[color=green]
> > Hello, when using cin.getline() with a char array, how should I specify[/color]
> the[color=green]
> > size (the second parameter of getline)?
> >
> > const int buffer_size = 256;
> > char buffer[buffer_size];
> >
> > std::cin.getline(buffer, std::streamsize(buffer_size));
> > or
> > std::cin.getline(buffer, buffer_size);
> > ?
> >
> > I know, I should use std::string and I usually do, but I still want to[/color]
> know[color=green]
> > the proper way. I am writing a program that will be probably converted[/color][/color]
to[color=blue]
> C[color=green]
> > so I ended up with char arrays instead of std::string, and the program[/color]
> uses[color=green]
> > a third-party api which is based on C.[/color]
>
> The C++ standard shows these prototypes:
>
> basic_istream<charT,traits>& getline(char_type* s, streamsize n);
> basic_istream<charT,traits>& getline(char_type* s, streamsize n,
> char_type delim);
>
> so I'd use 'streamsize' type.
>
> But with C, you don't have that type, use 'size_t' to specify
> object sizes or counts. And when calling library functions,
> simply use whatever type the prototype specifies.
>
>
> -Mike
>
>[/color]

Thanks alot for your reply, Mike. I will use std::streamsize.

/ William Payne


David Rubin
Guest
 
Posts: n/a
#4: Jul 22 '05

re: How should I specify size?


William Payne wrote:[color=blue]
>
> Hello, when using cin.getline() with a char array, how should I specify the
> size (the second parameter of getline)?
>
> const int buffer_size = 256;
> char buffer[buffer_size];
>
> std::cin.getline(buffer, std::streamsize(buffer_size));
> or
> std::cin.getline(buffer, buffer_size);
> ?[/color]

std::cin.getline(buffer, sizeof buffer);

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jack Klein
Guest
 
Posts: n/a
#5: Jul 22 '05

re: How should I specify size?


On Mon, 02 Feb 2004 20:02:02 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote in comp.lang.c++:
[color=blue]
>
> "William Payne" <mikas493_no_s_p_a_m_@student.liu.se> wrote in message
> news:bvm97i$443$1@news.island.liu.se...[color=green]
> > Hello, when using cin.getline() with a char array, how should I specify[/color]
> the[color=green]
> > size (the second parameter of getline)?
> >
> > const int buffer_size = 256;
> > char buffer[buffer_size];
> >
> > std::cin.getline(buffer, std::streamsize(buffer_size));
> > or
> > std::cin.getline(buffer, buffer_size);
> > ?
> >
> > I know, I should use std::string and I usually do, but I still want to[/color]
> know[color=green]
> > the proper way. I am writing a program that will be probably converted to[/color]
> C[color=green]
> > so I ended up with char arrays instead of std::string, and the program[/color]
> uses[color=green]
> > a third-party api which is based on C.[/color]
>
> The C++ standard shows these prototypes:
>
> basic_istream<charT,traits>& getline(char_type* s, streamsize n);
> basic_istream<charT,traits>& getline(char_type* s, streamsize n,
> char_type delim);
>
> so I'd use 'streamsize' type.
>
> But with C, you don't have that type, use 'size_t' to specify
> object sizes or counts. And when calling library functions,
> simply use whatever type the prototype specifies.
>
>
> -Mike[/color]

I disagree here, Mike. Since C++ requires a proper prototype in
scope, the conversion is automatic. The functional-like cast just
adds unnecessary clutter in this case.

My rule is never provide an explicit cast for an implicit automatic
loss-less conversion unless the circumstances are complex enough that
someone reading the code might be mislead.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Mike Wahler
Guest
 
Posts: n/a
#6: Jul 22 '05

re: How should I specify size?



"Jack Klein" <jackklein@spamcop.net> wrote in message
news:cu9u10dkb8nkh9e0hv37ej0bsb9cbcdo8b@4ax.com...[color=blue]
> On Mon, 02 Feb 2004 20:02:02 GMT, "Mike Wahler"
> <mkwahler@mkwahler.net> wrote in comp.lang.c++:
>[color=green]
> >
> > "William Payne" <mikas493_no_s_p_a_m_@student.liu.se> wrote in message
> > news:bvm97i$443$1@news.island.liu.se...[color=darkred]
> > > Hello, when using cin.getline() with a char array, how should I[/color][/color][/color]
specify[color=blue][color=green]
> > the[color=darkred]
> > > size (the second parameter of getline)?
> > >
> > > const int buffer_size = 256;
> > > char buffer[buffer_size];
> > >
> > > std::cin.getline(buffer, std::streamsize(buffer_size));
> > > or
> > > std::cin.getline(buffer, buffer_size);
> > > ?
> > >
> > > I know, I should use std::string and I usually do, but I still want to[/color]
> > know[color=darkred]
> > > the proper way. I am writing a program that will be probably converted[/color][/color][/color]
to[color=blue][color=green]
> > C[color=darkred]
> > > so I ended up with char arrays instead of std::string, and the program[/color]
> > uses[color=darkred]
> > > a third-party api which is based on C.[/color]
> >
> > The C++ standard shows these prototypes:
> >
> > basic_istream<charT,traits>& getline(char_type* s, streamsize n);
> > basic_istream<charT,traits>& getline(char_type* s, streamsize n,
> > char_type delim);
> >
> > so I'd use 'streamsize' type.
> >
> > But with C, you don't have that type, use 'size_t' to specify
> > object sizes or counts. And when calling library functions,
> > simply use whatever type the prototype specifies.
> >
> >
> > -Mike[/color]
>
> I disagree here, Mike. Since C++ requires a proper prototype in
> scope, the conversion is automatic. The functional-like cast just
> adds unnecessary clutter in this case.[/color]

But do we have any guarantee that 'streamsize' is at least as large
as e.g. 'int'?
[color=blue]
>
> My rule is never provide an explicit cast for an implicit automatic
> loss-less conversion unless the circumstances are complex enough that
> someone reading the code might be mislead.[/color]

I Agree.

-Mike


Martijn Lievaart
Guest
 
Posts: n/a
#7: Jul 22 '05

re: How should I specify size?


On Tue, 03 Feb 2004 18:13:14 +0000, Mike Wahler wrote:
[color=blue]
> But do we have any guarantee that 'streamsize' is at least as large
> as e.g. 'int'?[/color]

Actually not, 27.4.1 clause 2:

"The type streamsize is a synonym for one of the signed basic integral
types. It is used to represent the number of characters transferred in an
I/O operation, or the size of I/O buffers.266)"

So the standard allows signed char. In practice it should be at least the
signed couterpart to size_t, but I think this is a defect.

M4

Closed Thread


Similar C / C++ bytes