Mail Archives: djgpp/1997/04/21/16:30:55
In article <Pine DOT OSF DOT 3 DOT 96 DOT 970418062103 DOT 11389A-100000 AT alpha DOT hut DOT fi>,
tax AT cc DOT hut DOT fi says...
>On 17 Apr 1997, Mike Collins wrote:
>> Am I doing something wrong? Why does the following not work?
>> #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?
>
>File output in djgpp is buffered. Nothing is really written to the file
>until the file is closed, or fflush(), fseek() or rewind() is called.
>So you probably want to add the following line before the printf():
> fflush(fp);
>
Thanks, I'll give it a try.
>> 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
>> 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!
>
>From the DJGPP C Library reference:
>chsize
>Syntax
> #include <io.h>
> int chsize(int handle, long size);
>Description
> Just calls ftruncate (*note ftruncate::.).
>Return Value
> Zero on success, -1 on failure.
>
>ftruncate
>Syntax
> #include <unistd.h>
Do you need to #include<unistd> when using chsize()?
> int ftruncate(int file, off_t where);
>Description
> This function truncates FILE at WHERE length. This only works if
the
> file is closed right after this call.
>Return Value
> Zero for success, nonzero for failure.
>
>So, you'll have to close the file immediately after the truncation.
>I tested it, and it worked fine.
It's a bit of a nuicance, though. In my program, the file in
question contains a list of structures. The file-management
function calls a function that displays a screen-full of the
structures, then checks the keyboard. If it detects the delete
key, it calls a routine that deletes the structure
(highlighted on the screen) and and copies all following
structures into the slot before. Imagine the list of
structures as 1 2 3 4 5 6 7 8, and we want to delete number 4.
5 gets copied to the 4th position, 6 to the fifth, etc, and 8
to the seventh, so we end up with 1 2 3 5 6 7 8 8. At this
point, I want to trunkate the file to get rid of the extra 8.
The trunkation should logically be part of the 'delete'
routine, but it doesn't know the file name to re-open it, so I
don't really want to have to close / re-open it. I haven't
checked the C reference, but is this standard C - that
chsize() must be followed with close()?
I have never had to port a C program between compilers before,
but my impression was that the language was so standard that
it would be a trivial task. I have spent ages on doing it, and
I'm not there yet. Some of the trouble was that my program was
not in standard C (conio routines, etc) and some was because
the original program was under DOS and used keywords like
'huge' and 'far', (and I haven't even tackled the interrupt stuff yet),
but a lot of the trouble has been simply due to differences between the
compilers.
I must confess to being a little disillusioned.
Thanks for the answer, though.
Mike.
-----
Don't just hit "reply" - my E-mail address is bogus
to avoid automatic browsers from sending junk mail.
Please use collim'at'anubis'dot'network'dot'com
- Raw text -