Mail Archives: djgpp/2013/12/03/10:30:11
X-Authentication-Warning: | delorie.com: mail set sender to djgpp-bounces using -f
|
X-Received: | by 10.180.105.68 with SMTP id gk4mr1464232wib.0.1386083653396;
|
| Tue, 03 Dec 2013 07:14:13 -0800 (PST)
|
From: | "Rod Pemberton" <dont_use_email AT xnohavenotit DOT cnm>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Question about different behavior of fclose/fread on plain dos
|
| and windows.
|
Date: | Tue, 03 Dec 2013 10:17:21 -0500
|
Organization: | Aioe.org NNTP Server
|
Lines: | 102
|
Message-ID: | <op.w7ive7du5zc71u@localhost>
|
References: | <529D448A DOT 7050208 AT gmx DOT de>
|
NNTP-Posting-Host: | CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org
|
Mime-Version: | 1.0
|
X-Complaints-To: | abuse AT aioe DOT org
|
User-Agent: | Opera Mail/12.16 (Linux)
|
X-Notice: | Filtered by postfilter v. 0.8.2
|
Bytes: | 4051
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
On Mon, 02 Dec 2013 21:40:10 -0500, Juan Manuel Guerrero
<juan DOT guerrero AT gmx DOT de> wrote:
> Please inspect the code snippet below:
>
> #include <stdio.h>
>
> int main(void)
> {
> char buf_w[] = "pipapo";
> char buf_r[sizeof(buf_w)] = "";
> FILE *fw = fopen("file.txt", "w");
> FILE *fr = fopen("file.txt", "r");
> size_t bytes;
>
> bytes = fwrite(buf_w, sizeof(char), sizeof buf_w, fw);
> printf("fwrite(fw) %s (%d): %s\n", bytes != sizeof buf_w ? "KO" :
> "OK", bytes, buf_w);
> bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr);
> printf("fread(fr) %s (%d): %s\n", bytes != 0 ? "KO" : "OK", bytes,
> buf_r);
>
> fclose(fw);
> fseek(fr, 0, SEEK_SET);
> bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr); /* This cuases
> difficulties. */
> printf("fread(fr) %s (%d): %s\n", bytes != sizeof buf_r ? "KO" :
> "OK", bytes, buf_r);
>
> return 0;
> }
>
>
You'll likely have to change the code for DOS to something like this:
#include <stdio.h>
int main(void)
{
char buf_w[] = "pipapo";
char buf_r[sizeof(buf_w)] = "";
FILE *fw; /* don't open here */
FILE *fr; /* don't open here */
size_t bytes;
/* delete file if it exists */
if(fopen("file.txt","r")!=NULL)
{
remove("file.txt");
}
/* open after file deletion is confirmed */
fw=fopen("file.txt", "w");
fr=fopen("file.txt", "r");
bytes = fwrite(buf_w, sizeof(char), sizeof buf_w, fw);
printf("fwrite(fw) %s (%d): %s\n", bytes != sizeof buf_w ? "KO" : "OK",
bytes, buf_w);
bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr);
printf("fread(fr) %s (%d): %s\n", bytes != 0 ? "KO" : "OK", bytes,
buf_r);
fclose(fw);
fseek(fr, 0, SEEK_SET);
/* reopen read file */
freopen("file.txt", "r", fr);
bytes = fread(buf_r, sizeof(char), sizeof buf_r, fr); /* This cuases
difficulties. */
printf("fread(fr) %s (%d): %s\n", bytes != sizeof buf_r ? "KO" : "OK",
bytes, buf_r);
return 0;
}
That seems to work correctly for MS-DOS v7.10 and Windows SE
DOS console window. Sorry, I just realized I didn't check
v2.04 for that. I'd have to reboot and loose this reply to
you to check. Let me know if you want me to test that.
I tried it both with and without "file.txt". Some solutions
I attempted didn't update "file.txt" with a new "pipapo", but
were reading the old "pipapo" in "file.txt", i.e., the previously
saved text. So, I also edited "file.txt" changed "pipapo" to
"zzzzzz" to verify "pipapo" was actually being updated when the
program executed. Of course, the version above just deletes
"file.txt" each time.
One of the solutions I looked at which appeared promising at
first was changed fr's "r" mode to "a+", but it had the
"zzzzzz" problem. I'm guessing there is a possibility that an
"a+" mode for fr and the freopen() might be all that's needed.
So, that might be worth trying too. It'd be easier solution
if it works.
Hope that helps.
Rod Pemberton
- Raw text -