473,387 Members | 1,464 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.

How should I specify size?

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
Jul 22 '05 #1
6 1632

"William Payne" <mi******************@student.liu.se> wrote in message
news:bv**********@news.island.liu.se...
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.


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
Jul 22 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_0*****************@newsread1.news.pas.earthl ink.net...

"William Payne" <mi******************@student.liu.se> wrote in message
news:bv**********@news.island.liu.se...
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.


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


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

/ William Payne
Jul 22 '05 #3
William Payne wrote:

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);
?


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
Jul 22 '05 #4
On Mon, 02 Feb 2004 20:02:02 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:

"William Payne" <mi******************@student.liu.se> wrote in message
news:bv**********@news.island.liu.se...
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.


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


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

Jul 22 '05 #5

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:cu********************************@4ax.com...
On Mon, 02 Feb 2004 20:02:02 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++:

"William Payne" <mi******************@student.liu.se> wrote in message
news:bv**********@news.island.liu.se...
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.


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


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.


But do we have any guarantee that 'streamsize' is at least as large
as e.g. 'int'?

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.


I Agree.

-Mike
Jul 22 '05 #6
On Tue, 03 Feb 2004 18:13:14 +0000, Mike Wahler wrote:
But do we have any guarantee that 'streamsize' is at least as large
as e.g. 'int'?


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

Jul 22 '05 #7

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

Similar topics

0
by: Anthony Liu | last post by:
I cannot figure out how to specify a list of a particular size. For example, I want to construct a list of size 10, how do I do this?
3
by: Yeah | last post by:
I have an HTML table with two columns of text. Here's what it looks like: FIRST COLUMN (each cell is ID="date') Background orange, font Serif size 13px, aligned center SECOND COLUMN (each cell...
0
by: Lei Jiang | last post by:
I have a program that cost a lot of memory. During the load of the program, more and more memory is being allocated. I'd like to know if I specify a large initial memory size for the application,...
3
by: Derrick | last post by:
I am getting an out of memory exception when spinning up a few hundred threads all processing sizable amounts of XML. Is there any way to specify min/max heap as there is with the java -Mx128 type...
7
by: henrytcy | last post by:
Hi, How can I convert integer, for example 12113, to char array but without specify an array size in C programming? Thanks!
16
by: JD | last post by:
Hi guys What's the best way to specify font size using CSS? I try to avoid absolute units like pt and px because then users can't resize the fonts in IE, but % and em are a complete pain to use...
2
by: winstontuck | last post by:
I have different web pages that I want to automatically default to either landscape or portrait when printing, depending on the page. Can this be done with javascript.
6
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I want to specify a custom color for disabled text that is used by a class that inherits from ToolStripProfessionalRenderer. The code would look something like this: Public Class MyRenderer...
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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,...

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.