Greetings, I know it's usually a bad habit to include several questions in a
single message, but they are quite related.
I have declared and defined a function named readin, whose purpose is to get
input from stdin and save the result in a character array (provided the
input does not exceed the maximum limit). Somehow it feels natural that
readin will then have the type "char *" and return the obtained input to
whoever calls it (or rather, the address of the first element?). Such as
this:
char *readin();
However, in case anything goes wrong readin should be able to return an
error code to the function that called it.
I "solved" the problem with the following declaration of readin:
int readin (char *);
Which makes it possible to return -1, and the function that calls readin
always tests the return value in an if-clause. From my brief and
incomprehensibl e explanation, does this look like an acceptable solution -
or are there better, standardized ways to perform error testing? I'm new to
C and am therefore trying to pick up as few bad habits as possible. 11 1890
On Sun, 17 Jun 2007 10:02:18 GMT, Victor Lagerkvist wrote:
>However, in case anything goes wrong readin should be able to return an error code to the function that called it.
I "solved" the problem with the following declaration of readin:
int readin (char *);
Which makes it possible to return -1, and the function that calls readin always tests the return value in an if-clause. From my brief and incomprehensib le explanation, does this look like an acceptable solution - or are there better, standardized ways to perform error testing? I'm new to C and am therefore trying to pick up as few bad habits as possible.
Compare your solution to Microsoft's (it's just a comparison of
interfaces, not the off-topic discussion of a non C-Standard library):
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesT oRead,
LPDWORD lpNumberOfBytes Read,
LPOVERLAPPED lpOverlapped
); http://msdn2.microsoft.com/en-us/library/aa365467.aspx
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Victor Lagerkvist said:
<snip>
>
char *readin();
However, in case anything goes wrong readin should be able to return
an error code to the function that called it.
I "solved" the problem with the following declaration of readin:
int readin (char *);
Which makes it possible to return -1, and the function that calls
readin always tests the return value in an if-clause.
How does readin() know how much data it can safely store in the buffer
whose first byte is pointed to by its parameter?
From my brief
and incomprehensibl e explanation, does this look like an acceptable
solution - or are there better, standardized ways to perform error
testing? I'm new to C and am therefore trying to pick up as few bad
habits as possible.
My own strategy is much like yours - I generally return an int error
code from my functions, unless they are object-creating functions, in
which case I return either a pointer to the new object or NULL if the
object could not be created.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
Victor Lagerkvist wrote:
>
.... snip ...
>
I "solved" the problem with the following declaration of readin:
int readin (char *);
Which makes it possible to return -1, and the function that calls
readin always tests the return value in an if-clause. From my
brief and incomprehensibl e explanation, does this look like an
acceptable solution - or are there better, standardized ways to
perform error testing? I'm new to C and am therefore trying to
pick up as few bad habits as possible.
Sounds good. In fact, it is almost exactly what ggets does, which
returns EOF for EOF, 0 for good operation, and something positive
for i/o error. See:
<http://cbfalconer.home .att.net/download/>
--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com
Richard Heathfield wrote:
How does readin() know how much data it can safely store in the buffer
whose first byte is pointed to by its parameter?
I currently use #define to specify the largest amount of allowed characters.
Victor Lagerkvist wrote:
>
Richard Heathfield wrote:
How does readin() know how much data it can
safely store in the buffer
whose first byte is pointed to by its parameter?
I currently use #define to specify
the largest amount of allowed characters.
In that case, you can use fscanf.
/* BEGIN fscanf_input.c */
/*
** There are only three different values
** that can be assigned to the int variable rc
** from the fscanf calls in this program;
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some other input problem;
** ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line
** (a line consisting only of one newline character)
** was entered, and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** The newline character in the text line is replaced
** by a null character in the array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>
#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)
int main(void)
{
int rc;
char array[LENGTH + 1];
puts("The LENGTH macro is " xstr(LENGTH) ".");
do {
fputs("Enter any line of text to continue,\n"
"or just hit the Enter key to quit:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
} else {
printf("rc is %d. Your string is:%s\n\n", rc, array);
}
} while (rc == 1);
return 0;
}
/* END fscanf_input.c */
--
pete
Victor Lagerkvist <pl*******@gmai l.comwrites:
Richard Heathfield wrote:
>How does readin() know how much data it can safely store in the buffer whose first byte is pointed to by its parameter?
I currently use #define to specify the largest amount of allowed
characters.
Ok.
You said "currently" . Does that imply that you might later want it to
be more flexible, allowing the maximum size to vary for each call? If
so, you should consider allowing for that in the interface now.
On the other hand, perhaps the size will always be fixed, or perhaps
you'll want to allocate the space dynamically.
There are lots of possibilities. It can be helpful to anticipate any
changes and nail down the interface early, but that's not always
possible.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson wrote:
Victor Lagerkvist <pl*******@gmai l.comwrites:
>Richard Heathfield wrote:
>>How does readin() know how much data it can safely store in the buffer whose first byte is pointed to by its parameter?
I currently use #define to specify the largest amount of allowed characters.
Ok.
You said "currently" . Does that imply that you might later want it to
be more flexible, allowing the maximum size to vary for each call? If
so, you should consider allowing for that in the interface now.
Yes... I'm pondering whether or not it is a good idea to save all user input
in an array for future references. In that case it would be neat if the
stored arrays weren't larger than the sentences they hold (currently every
array have place for the largest amount of allowed characters, which might
be wasteful if the backlog is big). Would it suffice to first get the user
input and save it in a "big" array and then copy it's content in a smaller
one of the same size as the number of characters the user entered? It
doesn't seem perfect, but I can't think of anything else since it's
impossible to tell how huge the input is beforehand. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave Smithz |
last post by:
Hi there,
I have a php script that does some form input validation. As it verifies
each field if the field has incorrect data, it appends an error message to
an $error array. E.g.
if (an_error = true) {
$errors.="<tr><td>You have note entered a surname. This is mandatory date
of birth incorrectly.</td></tr>";
}
|
by: Luis |
last post by:
the assignment is to count the number of words in a txt file here is my code
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
int main ()
{
|
by: Matthew Jakeman |
last post by:
can anyone tell me why the following piece of code causes a segmentation fault please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus.identifier) ;
while(allMenus.identifier != '\0')
{
printf("Hello World") ;
|
by: Fatih BOY |
last post by:
Hi,
I want to send a report from a windows application to a web page like
'report.asp'
Currently i can send it via post method with a context like
local=En&Username=fatih&UserId=45&Firm=none
But the problem occures when i want to send a data with & sign (i.e:
Firm=F&B). I try to solve this problem with using boundary, but i failed.
Any idea!?
|
by: zhi lin |
last post by:
hi, guys
I am doing a c program which need me to print out the error message during
the file input, suppose we need to read in a
struct {
int pid;
char *name;
char *skill;
}
| |
by: gj_williams2000 |
last post by:
Not sure if this is the right board but it is in c...
I've got a console program written in c which receives key presses and
gets a Windows Virtual key code. I am trying to convert it to the exact
ascii value the user has typed. so if shift is down it should be a
capital otherwise lower case etc.
I started with this:
void printKey(int key)
|
by: Mark Waser |
last post by:
Hi all,
I'm trying to post multipart/form-data to a web page but seem to have
run into a wall. I'm familiar with RFC 1867 and have done this before (with
AOLServer and Tcl) but just can't seem to get it to work in Visual Basic. I
tried coding it once myself from scratch and then modified a class that I
found on a newsgroup (referenced below). Both seem to be doing the same
thing and neither works (or rather, they seem to work but the...
|
by: Rylios |
last post by:
I am trying to make a very basic text editor using a linked list,
fstream, sorting...
This is what i have so far
------------------------------------------------------------------------------------------------------------------------------------
The Linked.H --- Header file
#include <iostream>
using namespace std;
|
by: George Orwell |
last post by:
hay, Han frm china heer again...
ben readin sum more c unleashed book dick heathfields book...
still readin dick heathfields data structs cht but jumped ahead
to chad dixons cgi proggraming cht out of interest for
bit varietty... hard to focuss on length of data structs cht
vry drawn out longwindded imho...
small robusttness patch for pg. 400 DlExchange() function:
int DLExchange(DLLIST *ItemA, DLLIST *ItemB)
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |