write int to a file

Non-platform specific questions.

write int to a file

Postby 4erv' » Sat Feb 27, 2010 3:33 pm

How to write integer into a text file? "fputs" doesnt work. Or is there any way to convert int to string? :D
Gold Basketball 2008 - the first basketball game made with game editor.

User avatar
4erv'
 
Posts: 188
Joined: Sat Jul 07, 2007 10:52 am
Location: in Recycle Bin
Score: 10 Give a positive score

Re: write int to a file

Postby makslane » Sat Feb 27, 2010 6:26 pm

Use the fwrite functionm like this:

Code: Select all
FILE *f = fopen("namearq", "wb"); //Write binary flag
fwrite(&yourIntVar, 1, sizeof(int), f);
fclose(f);
Game Editor is an open source game creator software that's wants to pay it's developers to keep evolving.
If you like Game Editor, make a review!
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Re: write int to a file

Postby 4erv' » Sun Feb 28, 2010 6:38 pm

Thanks! :)
Gold Basketball 2008 - the first basketball game made with game editor.

User avatar
4erv'
 
Posts: 188
Joined: Sat Jul 07, 2007 10:52 am
Location: in Recycle Bin
Score: 10 Give a positive score

Re: write int to a file

Postby Hblade » Sun Feb 28, 2010 8:20 pm

To convert to a string (I think, havnt tried it) but..

I believe the form is to_s. I think it would be...
Code: Select all
var.to_s;


Not sure :/ but I know its to_s :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: write int to a file

Postby Bee-Ant » Mon Mar 01, 2010 5:48 pm

4erv' wrote:How to write integer into a text file? "fputs" doesnt work. Or is there any way to convert int to string? :D

1. Write to file
Code: Select all
FILE *write=fopen("name.ext","w");
fprintf(write,"%i",integer_variable);
fclose(write);

2. Convert int to string
Code: Select all
sprintf(string_var,"%i",int_var);

If you want the ASCII character from the int you want to convert :
Code: Select all
sprintf(string_var,"%i",&int_var);
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest