Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Writing an integer to a file

JavaStud2003JavaStud2003 Posts: 4Member
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.

Comments

  • GogiGogi Posts: 258Member
    : 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*:

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

    I assume you mean char* by string. ;)
  • JavaStud2003JavaStud2003 Posts: 4Member
    : : 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*:
    :
    : [code]
    : int number=7;
    : char buffer[8];
    : itoa(number,buffer,10);
    : [/code]
    :
    : I assume you mean char* by string. ;)
    :

  • stoberstober Posts: 9,765Member ✭✭✭
    : 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
    [code]
    int n = 123;
    FILE* fp = fopen(....)
    fprintf(fp,"%d",n);
    [/code]

  • DB1DB1 Posts: 1,142Member
    : : 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
    : [code]
    : int n = 123;
    : FILE* fp = fopen(....)
    : fprintf(fp,"%d",n);
    : [/code]
    :
    :

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




    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • antonsantons Posts: 117Member
    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???

  • stoberstober Posts: 9,765Member ✭✭✭
    : 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.
Sign In or Register to comment.