Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
FILE *p as a global var; fflush() in lite-c #471021
02/16/18 12:46
02/16/18 12:46
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
Hello,

There are several things which I cannot get to work (Zorro).

Consider the following code;
Quote:
FILE *p;

main ()
{
int tmp=3;
p=fopen("d:\myfile.csv",w+b);
fwrite(&tmp,sizeof(int),1,p);
fclose(p);
}

run()
{
int tmp;
p=fopen("d:\myfile.csv",rb);
fread(&tmp,sizeof(int),1,p);
printf(" n PB=%i", tmp);
fclose(p);
}


Questions:

1. If I skip closing the file in main() and re-opening it in run(), the result of printf() is 0.
Since *p is defined globally, I would expect it to keep its value, and then be able to read from the file p, still seemingly open.
Why doesn't that work?

2. to implement (1), If I do fflush(p) in main() instead of fclose(), I can see that the data are not physically written to a file (as would be expected).
Are there any nuances to it?

Thank you in advance for help.

Re: FILE *p as a global var; fflush() in lite-c [Re: Zheka] #471029
02/16/18 16:09
02/16/18 16:09
Joined: Apr 2008
Posts: 585
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 585
Austria
Skipping and not opening is not good, the file handle is still open with "w+b". You cant read a variable from such a file handle.

Re: FILE *p as a global var; fflush() in lite-c [Re: Petra] #471030
02/16/18 16:26
02/16/18 16:26
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
But it is written everywhere that w+ opens file for both - writing AND reading.
https://www.cprogramming.com/tutorial/cfileio.html

Where is the catch?


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1