Page 1 of 2

fwrite problem

PostPosted: Wed Aug 04, 2010 4:49 pm
by tzoli
I have a problem with fwrite.
This is my code:
Code: Select all
unsigned px = 0;
FILE *f = fopen("C:/ct.txt","r+");
fwrite(&px,"x",10,f);
fclose(f);

And the error:
Incompatible types:Cannot convert from "const * char" to "unsigned"
What can i do for fix it?

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 5:49 pm
by savvy
it means there something in there which isnt supposed to be, or that your missing a peice of info which seperates them, OR even that you have something splitting up something thats supposed to be together, but im not sure what. try rethinking the code from the start.

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 5:51 pm
by Game A Gogo
Actually, this just means you placed a variable that is a const*char (Constant string, a variable you declared that is a string) where an unsigned (An integer without negative values, I beleive...) should go

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 5:53 pm
by Game A Gogo
it would also be easier to help you, if you told us where this error happens (On which line? it always tells you thankfully!)

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 6:01 pm
by Fuzzy
What immediately stands out to me is that you used the wrong slash for windows and you should have escaped it.

FILE *f = fopen("C:\\ct.txt","r+");

Also, I wouldnt write to the root C folder like that. Put it in with your game.

FILE *f = fopen("C:\\somegame\\ct.txt","r+");

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 6:02 pm
by tzoli
Thanks.

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 6:24 pm
by tzoli
I have problem again :twisted:
Code: Select all
int px = 0;
FILE * f = fopen("C:\\ct.txt","w");
fwrite(&px,1,1,f);
fclose(f);

Aren't errors but not write the file... :(

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 7:47 pm
by makslane
You can create files only inside the game folder.

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 8:03 pm
by asmodeus
makslane wrote:You can create files only inside the game folder.

Why that? I don't understand it.

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 8:45 pm
by makslane
Security.

Re: fwrite problem

PostPosted: Wed Aug 04, 2010 9:27 pm
by asmodeus
makslane wrote:Security.

Okay, I see...
But you could make have possible to access files in sub directories, e.g. files from a "data" dir, being in the same dir as the game. Just not allowing for example "../" or "C:/".

Re: fwrite problem

PostPosted: Thu Aug 05, 2010 5:35 am
by tzoli
Okay...But can how can I write to file strings?
My codes doesn't running corectly. :(

Re: fwrite problem

PostPosted: Mon Aug 09, 2010 7:02 am
by tzoli
I have a problem whit my code:
Code: Select all
int px = 100;
FILE *f = fopen("ct.txt", "r+");
fwrite(&px, ymouse, sizeof(px), f);
fclose(f);

No errors but in the file wrote:
d `Ě €°g €°g  °g €ăÇ €°g €°g  °g °Č  €°g  °g Xnż s ˙˙˙˙ €ăÇ H ř H ř L ř č‹Â řÔÉ ¬ż ¬ż čě ŕsż ąĆ ŕĘÉ čüĂ 0şŔ °cĚ đ/É đż ČoČ

Re: fwrite problem

PostPosted: Mon Aug 09, 2010 10:02 am
by Bee-Ant
fwrite() would write text in binary code, if you want it to be "readable" use fprintf() instead.
Code: Select all
fprintf(f,"%d,",&ymouse);

Re: fwrite problem

PostPosted: Mon Aug 09, 2010 1:44 pm
by tzoli
Thanks!It's run correctly :D
Image