Jump to content
IGNORED

File handling cc65


Recommended Posts

I've started programming in cc65 and seem to have run into a small problem,

I'm reading data from one file, doing a conversion and writing the result to a second file.

 

The problem I have is if the file I want to write to exists I seem to be able to open it with fopen(outfile,"w");

but when I try to write to the file fputs(outstring, outfile); the computer hangs.

 

If the file doesn't exist, the same piece of code writes the data successfully.

 

I've tried opening the file in modes "a" , "r+" , "w+" none seem to work and fail when writing,

I've also used fseek to set the file pointer to the begining of the file.

 

I also tried remove(outfile); but it returns -1 which is a fail. (it does say remove may not be implemented in all instances)

 

Anyone know of a way round this, I know I could use a unique filename each time, but then I would

have to delete unwanted files in DOS.

 

On Unix systems using C, if you open a file for writing and it exists, it is deleted and recreated empty ready for writing (I tested this to be sure)

  • Like 1
Link to comment
Share on other sites

Using SDX latest released version

 

This is the code, it's not complete, but you can probably see what I'm trying to do, filenames have already

been set. Initially, I didn't do the fexist test, but when it failed, I was trying to overcome the problem.

 

 

int process(void) // process in/out files
{
    char inbyte[10]; // max 10 bytes at a time
    char outstring[255]; // formatted output string
    int i,flag,header;
    int fsize,fcount;
    finput=fopen(infile,"r");
    if(finput == NULL)
        return(-1);
    if(fexist)
    {
        foutput=fopen(outfile,"a+"); // file exists
        fseek(foutput,0L,SEEK_SET); // set file pointer to begining
    }
    else
        foutput=fopen(outfile,"w"); // create new

    if(foutput == NULL)
    {
        fclose(finput);
        return(-1);
    }
    flag = 0;
    header=0;
    fseek(finput,0L,SEEK_END); // get file size
    fsize = ftell(finput);
    rewind(finput);
    
    fcount=0;
    do
    {
        if(header == 0) // get header 6 bytes
        {
            for(i=0;i<6;i++)
                inbyte=fgetc(finput);
            sprintf(outstring,"%d DATA %0.3u,%0.3u,%0.3u,%0.3u,%0.3u,%0.3u\n", \
                lineno,inbyte[0],inbyte[1],inbyte[2],inbyte[3],inbyte[4],inbyte[5]);
            fputs(outstring,foutput);
            fcount += 6;
            header = 1;
        }
        flag=1;
    }while(!flag);
    fclose(finput);
    fclose(foutput);
    return(0);
}

 

Link to comment
Share on other sites

It might be a bug that remove() doesn't work. On the Atari, it could just call XIO with command code 33.

 

fseek() not working could either be a problem with SDX (not likely as its filesystem is fast enough for seeks) or with CC65's implementation (more likely).

 

But the weird thing is that if you open a file through CIO, if it already exists, it is deleted! I (re)learned that the hard way about a year ago when I was working with real floppies again and copied a file to itself. That was DOS 2.5 though.

 

Edited by ivop
Link to comment
Share on other sites

fseek works fine in the code where I get the size of the input file, and I know as you found out, opening a file from BASIC will delete

the file on disk if it exists, so this must be a "quirk" of cc65's implementation.

 

Now here's thing, I just tried a small piece of code using open instead of fopen and it does delete the file and create an empty one

here's the code that works.

So it looks like its how fopen works

 

I first wrote 1 line, then 3 lines, then 2 lines and the correct number of lines were there each time.

 

#include <unistd.h>
#include <fcntl.h>
 
int main()
{
    int filedesc = open("testfile.txt", O_WRONLY);
    if(filedesc < 0)
        return 1;
 
    write(filedesc,"This will be output to testfile.txt\n", 36);
    write(filedesc,"This will be output to testfile.txt\n", 36);
     close(filedesc);
    return 0;
}

Edited by TGB1718
Link to comment
Share on other sites

Many thanks for your replies, have to hold my hand up and say, My Fault, I did some

file checking earlier in my code to see if it was ok to overwrite a file if it already existed.

 

It did the check by trying to open the file, if it opened, then was asked to overwrite.

big oops, forgot to close the file !!!

 

Just a silly mistake, but hey, I'm trying to get back into this.

 

Sorry ?

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...