please help with this program 
Author Message
 please help with this program

Quote:

> Hi,  I've written this file,   with the help of this NG!!!,  but
> I can't seem still to get it to work.

> I run it and enter a filename, which hold account details:
> such as:

> 123 arez 23 56.79

> with a space between each field,
> but the result is :

> 0 transactions in total

> can someone help me
> cheers

> arez

> ****************
 [...]
>  while ( fscanf( fptr, "%d:%s:%d:%f\n",  &new_trans->account_num,

                            ^  ^  ^
In your data file, you said the fields were separated by spaces.
Remove the colons.

Quote:
> &new_trans->name,

  ^^^^^^^^^^^^^^^^
s/b "new_trans->name" not "&new_trans->name"

[...]

That should help.

--

--



Wed, 17 Oct 2001 03:00:00 GMT  
 please help with this program
Groovy hepcat arez was jivin' on Sat, 01 May 1999 05:55:31 GMT in
comp.lang.c.moderated.
please help with this program's a cool scene! Dig it!

Quote:
>I run it and enter a filename, which hold account details:
>such as:

>123 arez 23 56.79

>but the result is :

>0 transactions in total

>#include <stdio.h>
>#include <stdlib.h>
>#include <errno.h>

>#define MAX 80

>typedef struct transactions{
>      int account_num;
>      char name[MAX];
>      int amount;
>      float balance;
>      struct transactions *next;
>         } Transnode;

>Transnode *read_transactions ( Transnode *head, const char *filename)
>{
> Transnode *tmp_ptr, *new_trans;
> FILE *fptr;

> if ( (fptr = fopen(filename, "r")) == NULL)
> {
>   perror("file open");

  As a style issue, it would be better if you passed a more
"error-like" message to perror(). "file open" does not look like an
error message to me. AAMOF, it might look like the program is saying
that the file was successfully opened. A clearer message would be
better, such as "Error: could not open file.".

Quote:
>   exit(1);

  Wrong! You cannot portably pass a value other than 0, EXIT_SUCCESS
or EXIT_FAILURE to exit().

Quote:
> }

> new_trans = (Transnode *) malloc ( sizeof(Transnode) );

  Do *not* cast the pointer returned from malloc(). Disregard those
people who told you to use a cast here. They were wrong. The cast is
required in C++, but you're writing C, aren't you? In C, this cast is
considered bad form. It hides a potential error.

Quote:
> while ( fscanf( fptr, "%d:%s:%d:%f\n",  &new_trans->account_num,

                           ^  ^  ^
  Here's the cause of your problem (at least, the one you asked about
in this thread). Your file does not contain any colons, yet you have
told fscanf() to look for colons in the file, thus it returns less
than the number expected, quitting before even entering the body of
the loop.

Quote:
>&new_trans->name,
>                &new_trans->amount, &new_trans->balance) ==4)
> {
>   tmp_ptr = head;
>   head = new_trans;
>   new_trans->next = tmp_ptr;
>   new_trans = (Transnode *) malloc ( sizeof(Transnode) );

  Again, don't cast the pointer returned from malloc().
  Anyhow, this seems like an... um... unconventional algorithm. At the
end of this block you have an extra Transnode which you then free when
the loop exits, and head actually points to the last node instead of
the first. It seems a little backward.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============

--



Tue, 23 Oct 2001 03:00:00 GMT  
 please help with this program


Quote:
> Groovy hepcat arez was jivin' on Sat, 01 May 1999 05:55:31 GMT in
> comp.lang.c.moderated.
> please help with this program's a cool scene! Dig it!
> >   perror("file open");

>   As a style issue, it would be better if you passed a more
> "error-like" message to perror(). "file open" does not look like an
> error message to me. AAMOF, it might look like the program is saying
> that the file was successfully opened. A clearer message would be
> better, such as "Error: could not open file.".

One comment.  perror() adds an error message derived from the setting of
the "errno" variable, right?  It's the same string as you get from
strerror(errno).  Therefore, the output would include an error message
anyway, so wouldn't another error message be extraneous?

I have on occasion simply passed the file name to perror() after a
failed fopen().  For file <Filename>, The resulting message tends to
look something like this:

        <Filename>: File not found

Admittedly, fopen() is not guaranteed to set errno on failure, so it
might be best to set errno to 0 first, and if it is still 0 on error,
*then* write a generic error message like you suggest.  However, it
might then be best to use fprintf(), since the error message perror()
would then append would not be meaningful anyway.

Yngvar
--



Wed, 24 Oct 2001 03:00:00 GMT  
 please help with this program

Quote:



> <snip>
> > Admittedly, fopen() is not guaranteed to set errno on failure, so it
> > might be best to set errno to 0 first, and if it is still 0 on error,
> > *then* write a generic error message like you suggest.  However, it
> > might then be best to use fprintf(), since the error message perror()
> > would then append would not be meaningful anyway.
> That is not a good idea.  It might work, it might not.  it is nonportable.
> Functions for which the standard does not document the use of errno are
> allowed to return with errno set to nonzero values even if they succeeded.
> To test whether or not fopen() succeeded, the return value should be
> examined.

I never said that it shouldn't.  I *did* point out that fopen() might
not set errno even if it failed, so I thought it self-evident that the
return value had to be checked anyway.

I was only making suggestions for how to use perror() provided that an
error already *had* been detected.

Yngvar
--



Fri, 09 Nov 2001 03:00:00 GMT  
 please help with this program

Quote:



> <snip>
> > Admittedly, fopen() is not guaranteed to set errno on failure, so it
> > might be best to set errno to 0 first, and if it is still 0 on error,
> > *then* write a generic error message like you suggest.  However, it
> > might then be best to use fprintf(), since the error message perror()
> > would then append would not be meaningful anyway.
> That is not a good idea.  It might work, it might not.  it is nonportable.
> Functions for which the standard does not document the use of errno are
> allowed to return with errno set to nonzero values even if they succeeded.
> To test whether or not fopen() succeeded, the return value should be
> examined.

I never said that it shouldn't.  I *did* point out that fopen() might
not set errno even if it failed, so I thought it self-evident that the
return value had to be checked anyway.

I was only making suggestions for how to use perror() provided that an
error already *had* been detected.

Yngvar
--



Fri, 09 Nov 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. PLEASE help UDP client programming

2. please help with this program

3. Please Help With C Program

4. Please help with C program

5. please help with this program

6. Please help beginner with programming problem

7. Please Help with this program

8. Oh gurus, please help getting VC program into DLL

9. Please help S3- card programming under TC?

10. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

11. Please help, question about programming in C to access COM1/COM2 in my FreeBSD.

12. Please help!!!!Please help!!!!Please help!!!!

 

 
Powered by phpBB® Forum Software