Re: [cc65] Atari file i/o more info

Date view Thread view Subject view

From: Christian Groessler (cpg_at_aladdin.de)
Date: 2002-05-16 17:45:07


Hi,

On 05/14/2002 02:28:36 PM MST "Shawn Jefferson" wrote:
>
>Ok, I've done some more testing on this and here is what I found:
>
>Opening a file in SpartaDOS with the "r+" flag won't trigger feof
>during a read.  AtariDOS 2.5 seems to work fine.
>
>Reading until EOF, then trying to write to the file doesn't seem
>work, even in AtariDOS.

The feof() works fine here on both DOSes (like you said in your last
mail), but with my test, writing after reaching eof did not work in
AtariDOS but did work with SpartaDOS.

fyi, here's my test program:

----------------
/*
 * feof test program
 * 2002, cpg
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <conio.h>

char text[] = "This is the file contents1\n";

int main(int argc, char **argv)
{
    char *filename,*x;
    char buf[20];
    FILE *file;
    size_t n;

    if (argc <= 1) {
        printf("\nfilename: ");
        x = fgets(buf,19,stdin);
        printf("\n");
        if (!x) {
            return(0);
        }
        filename = x;
    }
    else {
        filename = *(argv+1);
    }

    printf("Length of data: %d\n", strlen(text));

    /* create new file and write something into it */
    file = fopen(filename,"wb");
    if (!file) {
        fprintf(stderr,"cannot open %s: %s\n",filename,strerror(errno));
        return(1);
    }

    if (fwrite(text, 1, strlen(text), file) != strlen(text)) {
        fprintf(stderr,"short write, aborted\n");
        fclose(file);
        return(1);
    }
    fclose(file);

    /*file = fopen(filename,"r");*/
    file = fopen(filename,"r+");
    if (!file) {
        fprintf(stderr,"cannot open %s: %s\n",filename,strerror(errno));
        return(1);
    }

    while (! feof(file)) {
        n = fread(buf, 1, 2, file);
        printf("%ld bytes read: ", (long)n);
        if (n == 1) printf("0x%02X", buf[0]);
        else if (n == 2) printf("0x%02X, 0x%02X", buf[0], buf[1]);
        printf("\n");
    }

#if 1
#define WHATS_WRITTEN "<-- even more data -->"

    /* now write s/th */
    printf("len to write: %d\n", strlen/*sizeof*/(WHATS_WRITTEN));
    n = fwrite(WHATS_WRITTEN, 1, strlen/*sizeof*/(WHATS_WRITTEN), file);
    printf("wrote additional %d bytes\n", n);
#endif

    fclose(file);
    printf("hit return to quit");
    cgetc(); printf("\n");
    return(0);
}
----------------

regards,
chris

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-05-16 17:45:31 CEST