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

opinions on code

I've written a function that reads multiple
lines and puts it in a string.
What do you think of it, is it correct?
Thanks!

#define BUFSIZE 1024
int read_multiline(char **str)
{
char inputbuf[BUFSIZE];
char *tmp;
size_t size = BUFSIZE;

*str = malloc(size);
if (*str == NULL)
return -1;

(*str)[0] = '\0';
fgets(inputbuf, BUFSIZE, stdin);
while (inputbuf[0] != '\n') {
/* Allocate more memory if needed */
if (strlen(*str) + strlen(inputbuf) >= size) {
size += BUFSIZE;
tmp = realloc(*str, size);
if (tmp == NULL) {
free(*str);
return -1;
}
else
*str = tmp;
}
strcat(*str, inputbuf);
fgets(inputbuf, BUFSIZE, stdin);
}

return 0;
}
Jun 11 '06 #1
2 1434

jaso wrote:
I've written a function that reads multiple
lines and puts it in a string.
What do you think of it, is it correct?
Thanks!

#define BUFSIZE 1024
int read_multiline(char **str)
{
char inputbuf[BUFSIZE];
char *tmp;
size_t size = BUFSIZE;

*str = malloc(size);
if (*str == NULL)
return -1;

(*str)[0] = '\0';
fgets(inputbuf, BUFSIZE, stdin);
while (inputbuf[0] != '\n') {
/* Allocate more memory if needed */
if (strlen(*str) + strlen(inputbuf) >= size) {
size += BUFSIZE;
tmp = realloc(*str, size);
if (tmp == NULL) {
free(*str);
return -1;
}
else
*str = tmp;
}
strcat(*str, inputbuf);
fgets(inputbuf, BUFSIZE, stdin);
}

return 0;
}


Why not test it first and then ask us ? But since you decided to ask
first , it would have helped if you had given more details on what the
function is supposed to do. For example I take it that the function is
supposed to return if it reads an empty line.

I have spotted 2 mistakes and there may be more:
1) You do not check the return value of fgets() therefore you won't
know
if EOF has been reached.
2) It is possible , albeit unlikely , that a line will have size
exactly BUFSIZE-1
so your programme will put the line into inputbuf[] and when the loop
gets repeated
it will read '\n' and the function will exit although an empty line has
not been read.

Apart from these I note also that your algorithm is inefficient.
There's no reason
to put the read lines first inside inputbuf[] and then copy them into
your main buffer ;
you should put them straight into the main buffer as you read them.
Trying reading
one character at a time using getchar().

Jun 11 '06 #2

sp****@gmail.com wrote:
jaso wrote:
I've written a function that reads multiple
lines and puts it in a string.
What do you think of it, is it correct?
Thanks!

#define BUFSIZE 1024
int read_multiline(char **str)
{
char inputbuf[BUFSIZE];
char *tmp;
size_t size = BUFSIZE;

*str = malloc(size);
if (*str == NULL)
return -1;

(*str)[0] = '\0';
fgets(inputbuf, BUFSIZE, stdin);
while (inputbuf[0] != '\n') {
/* Allocate more memory if needed */
if (strlen(*str) + strlen(inputbuf) >= size) {
size += BUFSIZE;
tmp = realloc(*str, size);
if (tmp == NULL) {
free(*str);
return -1;
}
else
*str = tmp;
}
strcat(*str, inputbuf);
fgets(inputbuf, BUFSIZE, stdin);
}

return 0;
}


Why not test it first and then ask us ? But since you decided to ask
first , it would have helped if you had given more details on what the
function is supposed to do. For example I take it that the function is
supposed to return if it reads an empty line.

I have spotted 2 mistakes and there may be more:
1) You do not check the return value of fgets() therefore you won't
know
if EOF has been reached.
2) It is possible , albeit unlikely , that a line will have size
exactly BUFSIZE-1
so your programme will put the line into inputbuf[] and when the loop
gets repeated
it will read '\n' and the function will exit although an empty line has
not been read.

Apart from these I note also that your algorithm is inefficient.
There's no reason
to put the read lines first inside inputbuf[] and then copy them into
your main buffer ;
you should put them straight into the main buffer as you read them.
Trying reading
one character at a time using getchar().


Another remark: your code seems to rely on the assumption that a
character with value 0 won't be read from stdin. This is not guaranteed
and it's easy enough to modify your code so that it doesn't rely on
that assumption.

Jun 11 '06 #3

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

Similar topics

35
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when...
2
by: Kari Laitinen | last post by:
During the past 15 years I have been writing computer programs with so-called natural names, which means that the names (identifiers, symbols) in progarms are constructed of several natural words....
4
by: xeys_00 | last post by:
Well, I'm auditing CS164 again. C++ part 1, basically. I'd like to know if I can submit some code to the group and get some opinions, as the instructor is not going to do anything more than let me...
3
by: Jack Klein | last post by:
I'm looking for opinions on a C technique I, and others, have used successfully in the past. While some people swear by, apparently others swear at it. Assume a part of a program too large to...
3
by: John Doe | last post by:
I've been doing some reading/research on parsing simple configuration files through C, and have heard various opinions on the matter. I'd like to solicit some opinions and design criteria (as well...
3
by: CDMAPoster | last post by:
A.K.A. Is Double Dating a bad thing :-)? My post from several hours ago may have gotten lost so please forgive me if something similar to this shows up twice. From a modular programming class I...
10
by: John Swan | last post by:
Please, I have just created this site and am wondering what your opinion is from both professionals and amatures or the curious alike. Any opinions? www.integrated-dev-sol.co.uk Remove 123...
0
by: Sammy | last post by:
What I like about Inline code model is that you can use the VS 05 designer to create events for me (similar to opening a page in the design mode and double-clicking on a button). Example: select...
13
by: Miro | last post by:
Ok I have been slowely - and ever so slowely teaching myself VB.net Currently I have created an MDB file by code, and added fields to the MDB file by code. I like this solution because, ( im...
5
by: JT | last post by:
Hi, I would like opinions on a shareware issue. If shareware uses an online database to hold registration codes, which get copied locally and therefore only needs to check online if the...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
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,...
0
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...

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.