Mail Archives: djgpp/1997/04/19/03:38:13
From: | av568 AT rgfn DOT epcc DOT edu (Beautiful Savior Lutheran Church)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: DJGPP BUG ?
|
Date: | 18 Apr 1997 20:18:47 GMT
|
Organization: | The Rio Grande Free-Net, El Paso Community College, El Paso, TX
|
Lines: | 74
|
Message-ID: | <5j8kv7$5gk@news.epcc.edu>
|
References: | <5j5n1j$lph AT news DOT network DOT com>
|
NNTP-Posting-Host: | rgfn.epcc.edu
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Mike Collins (e-mail DOT address AT end DOT of DOT text) wrote:
> Am I doing something wrong? Why does the following not work?
> (I already deleted the original, so what I am writing here is
> not
> compiler tested, but you'll get the idea)
> #include <stdio.h>
> #include <io.h>
> main()
> { FILE *fp;
> fp = fopen("junk", "wb");
> fwrite("abcdefghijklmnopqrstuvwxyz", 26, 1, fp);
> // examination of the file at this point shows that it
> // contains 26 characters of the alphabet. To this point,
> // it works
> printf("%d", (int)(filelength(fileno(fp)))); // prints "0"
> // - WHY?
> fclose(fp);
> fp = fopen("junk", "rb+");
> printf("%d", (int)(filelength(fileno(fp))));
> // prints 26 as expected
> fwrite("abcdefghijklmnopqrstuvwxyz", 26, 1, fp);
> // examination of the file at this point shows that it
> // contains 52 characters of the alphabet - Fine!
> printf("%d", (int)(filelength(fileno(fp))));
> // still prints 26!
> }
> In my application, a file is written to, then its handle is
> passed to a function which needs to know the length of the file.
> This function cannot close and reopen the file, however, because
> it does not know the filename, which is derived in the first
> place by a fairly complex process (reading bits of other files)
> I don't want to have to go through developing the name of the
> file in the called function.
> This works fine under my 16-bit DOS-based compiler (Power-C by
> MIX).
I *think* that the problem is that you're not switching to read mode
correctly, which (AFAICR) must be explicitly done in DJGPP with either an
fseek(file, 0, SEEK_CUR) or an fflush(file)
> Another thing that works under Power-C but did not under DJGPP
> was to do with trunkation of a file using chsize(). The
> returned value was different if I closed the file with
> fclose(fp) (returned zero) or with close(fileno(fp)) (returned
fclose() returns zero on success, and EOF on error. close(fileno(file))
should (AFAIK) *NOT* be called to close a file opened with fopen().
> something else, but I can't remember what), but the trunkation
> didn't work in either case. I finally solved it by opening a
> second file, copying the first file into it up to the trunkation
> point, then closing both, deleting the original and renaming the
> new file to the original's name, and that works - but it's
> cumbersome!
I wrote a small program to add automatically add entries to the Info dir
node, and it uses this line to truncate:
ftruncate(fileno(dirfile), ftell(dirfile)-1);
which works perfectly. According to the libc.a docs, chsize() just calls
ftruncate(), so it is impossible to determine your problem without more
information. However, you may be forgetting this: you *must* call
fclose() *immediately* after truncating, or it will *not* work.
--
Beautiful Savior Evangelical Lutheran Church - av568 AT rgfn DOT epcc DOT edu
We have what you need-- A Savior!
- Raw text -