[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 2 Applesoft BASIC questions/1 Hyper C question



In article <9406081756.AA25635@phy.mtu.edu>,
Sean M. McAfee <smmcafee@phy.mtu.edu> wrote:
>Finally, I remembered Hyper C.  I was stopped short before getting anywhere,
>though, since I couldn't get the most rudimentary file I/O to work.  Can
>anyone familiar with Hyper C tell me what's wrong with the following program,
>which doesn't do a damn thing?
>
>#include <std.h>
>main()
>{
>    FILE *a;
>    int i;
>
>    create(a,"THISFILE",0);
>    fprintf(a,"Hello world!\n");
>}

Try this one:

main()
{
    FILE a; /* HyperC doesn't need pointers, though they won't matter */

    a=create("THISFILE");
#ifdef MUCH_LESS_OVERHEAD
    puts(a,"Hello world!\n");     /* much faster than fprintf */
#else
    fprintf(a,"Hello, world!\n");
#endif
    close(a); /* not closing a file isn't very friendly */
}

>I later tried putting the create() inside a printf() to see the return value,
>which turned out to be -1; according to the documentation, this means that the
>file was created successfully.

Really? The documentation I have says clearly that create() returns the
ProDOS file descriptor, and -1 means failure (or 0 if you're using Opix).

>After executing the program, though, no new
>file was to be found.  Help!

Of course not. Your use of create() could possibly crash the entire system
as you pass an uninitialized pointer to create() where it expects a pointer
to the pathname string. You could hit a softswitch accidentally, which is
likely to be desastrous.

Your code won't make sense under any I/O library, btw. You pass the pointer
a to create() by value, not by reference. That is, the contents of the
variable a can't be affected by create()!

Take a look at the sample code provided with HyperC - it's an excellent
example of how to access files.

- Wulf

-- 
 ______________________________________________________
|                                                      |
| Wulf Hofbauer | zcaa1122@rpool1.rus.uni-stuttgart.de |
|                                                      |