C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Writing an integer to a file Posted by JavaStud2003 on 6 Aug 2005 at 1:17 AM
Can anyone please help me to write an integer to a file. I know how to write a character and a string to a file but I can't find anything that says how to write an integer to a file. Either that or convert an integer into a string. There are many resources that tell me how to convert a string to an integer but none that say how to convert an integer to a string. Thanks a lot for your help.
Report
Re: Writing an integer to a file Posted by Gogi on 6 Aug 2005 at 2:13 AM
: Can anyone please help me to write an integer to a file. I know how to write a character and a string to a file but I can't find anything that says how to write an integer to a file. Either that or convert an integer into a string. There are many resources that tell me how to convert a string to an integer but none that say how to convert an integer to a string. Thanks a lot for your help.
:

Try this to convert from integer to char*:

    int number=7;
    char buffer[8];
    itoa(number,buffer,10);


I assume you mean char* by string. ;)
Report
Re: Writing an integer to a file Posted by JavaStud2003 on 6 Aug 2005 at 2:56 AM
: : Can anyone please help me to write an integer to a file. I know how to write a character and a string to a file but I can't find anything that says how to write an integer to a file. Either that or convert an integer into a string. There are many resources that tell me how to convert a string to an integer but none that say how to convert an integer to a string. Thanks a lot for your help.
: :
:
: Try this to convert from integer to char*:
:
:
:     int number=7;
:     char buffer[8];
:     itoa(number,buffer,10);
: 

:
: I assume you mean char* by string. ;)
:

Report
Re: Writing an integer to a file Posted by stober on 6 Aug 2005 at 3:56 AM
: Can anyone please help me to write an integer to a file. I know how to write a character and a string to a file but I can't find anything that says how to write an integer to a file. Either that or convert an integer into a string. There are many resources that tell me how to convert a string to an integer but none that say how to convert an integer to a string. Thanks a lot for your help.
:

Look in any c programming book or google and you will find how to use printf() to store any data type to a file.
http://www.mkssoftware.com/docs/man1/printf.1.asp

Here is an example of storing an integer
int n = 123;
FILE* fp = fopen(....)
fprintf(fp,"%d",n);


Report
Re: Writing an integer to a file Posted by DB1 on 6 Aug 2005 at 4:42 AM
: : Can anyone please help me to write an integer to a file. I know how to write a character and a string to a file but I can't find anything that says how to write an integer to a file. Either that or convert an integer into a string. There are many resources that tell me how to convert a string to an integer but none that say how to convert an integer to a string. Thanks a lot for your help.
: :
:
: Look in any c programming book or google and you will find how to use printf() to store any data type to a file.
: http://www.mkssoftware.com/docs/man1/printf.1.asp
:
: Here is an example of storing an integer
:
: int n = 123;
: FILE* fp = fopen(....)
: fprintf(fp,"%d",n);
: 

:
:

Or if you need to write the integer in a binary file..
void writeInteger(int integer)
{
	FILE *fp = fopen("test.dat", "wb");
	if( fp )
	{
		fwrite(&integer, sizeof(int), 1, fp);
		fclose(fp);
	}
}





To understand recursive, first you need to understand recursive

Report
Re: Writing an integer to a file Posted by antons on 7 Aug 2005 at 11:38 PM
What is a binary file, and why would you want to write an integer to it? I understand a binary file to contain instructions that can be executed by the computer???

Report
Re: Writing an integer to a file Posted by stober on 8 Aug 2005 at 2:29 AM
: What is a binary file, and why would you want to write an integer to it? I understand a binary file to contain instructions that can be executed by the computer???
:
:

That is one kind of binary file, but the binary files that programs create are normally just the computer's internal representation of data objects. Contrast this with text files, which are easily readable by any text program such as Notepad. Writing and reading data to/from binary files is a lot faster than doing the same thing with text files and binary files are usually smaller than text files. In many cases it doesn't make sense to write data out into text files -- wave and bitmaps are just two examples.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.