TGB1718 Posted April 9, 2020 Share Posted April 9, 2020 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) 1 Quote Link to comment Share on other sites More sharing options...
ilmenit Posted April 9, 2020 Share Posted April 9, 2020 (edited) Hi. Can you post the code? What DOS are you using? Edited April 9, 2020 by ilmenit Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted April 9, 2020 Author Share Posted April 9, 2020 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); } Quote Link to comment Share on other sites More sharing options...
ivop Posted April 9, 2020 Share Posted April 9, 2020 (edited) 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 April 9, 2020 by ivop Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted April 9, 2020 Author Share Posted April 9, 2020 (edited) 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 April 9, 2020 by TGB1718 Quote Link to comment Share on other sites More sharing options...
drac030 Posted April 9, 2020 Share Posted April 9, 2020 @TGB1718 Could you provide the most concise example of a program, which hangs, in both source and compiled form? Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted April 10, 2020 Author Share Posted April 10, 2020 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 ? 1 Quote Link to comment Share on other sites More sharing options...
ilmenit Posted April 10, 2020 Share Posted April 10, 2020 I'd recommend to use file functions from <fcntl.h> anyway. They are smaller than stdio ones and fXXXX ones use them internally. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.