Page 1 of 1

write int to a file

PostPosted: Sat Feb 27, 2010 3:33 pm
by 4erv'
How to write integer into a text file? "fputs" doesnt work. Or is there any way to convert int to string? :D

Re: write int to a file

PostPosted: Sat Feb 27, 2010 6:26 pm
by makslane
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);

Re: write int to a file

PostPosted: Sun Feb 28, 2010 6:38 pm
by 4erv'
Thanks! :)

Re: write int to a file

PostPosted: Sun Feb 28, 2010 8:20 pm
by Hblade
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

Re: write int to a file

PostPosted: Mon Mar 01, 2010 5:48 pm
by Bee-Ant
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);