delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/04/05:35:25

From: fesenko AT pacific DOT net DOT sg (Victor)
Newsgroups: comp.os.msdos.djgpp
Subject: open() question
Date: Fri, 01 Aug 1997 14:48:03 GMT
Organization: Subscriber, Pacific Internet, Singapore
Lines: 39
Message-ID: <33e1f561.1686433@news.pacific.net.sg>
NNTP-Posting-Host: dyn123ppp119.pacific.net.sg
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I'm having a problem with open() function. As I understand using
open("file.dat",O_RDWR|O_BINARY|O_CREAT)  a new file has to be created
and if the file already existed, it would be rewritten. A problem
shows when a file existed which had a bigger size than the file
created. Then the file is rewritten, but it still shows the old size
(which is bigger) and contains the remnants of an old file in the end.
Other ways to solve this , e.g. using creat(), or using unlink()
before open(), weren't successful as well.

This is a portion of the code :

   strcpy(fname,which); strcat(fname,".bmp");
   fp=open(fname,O_RDWR|O_CREAT|O_BINARY,S_IWUSR);
   if(fp == -1){ sprintf(mess," Error : can't open file '%s' [OK]
",fname);
                 Message(mess,ENT,HRed,MIDDLE);
                 free(Buffer); 
                 return(0);
               } 
   WriteBMPHeader(fp);
   
  { unsigned char *im2; int i,j; 
    int Lines=ScanState.Info.Lines,
WidthPixels=ScanState.Info.WidthPixels;
  im2=malloc(Picsize);  // reverse the lines order
  for(i=(Lines-1)*WidthPixels,j=0;i>=0;i-=WidthPixels,j+=WidthPixels)
    memcpy(im2+i,Buffer+j,WidthPixels);
      write(fp,im2,Picsize);
      free(im2);
  }
   
   close(fp);
   free(Buffer); 
   Message(" Scanning, please wait... ",E,HRed,MIDDLE);
   return(1);

Any suggestions appreciated.

Victor Fesenko.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019