Thread: writing variable (int and string) values...

  1. #1
    Unregistered
    Guest

    Smile writing variable (int and string) values...

    Would someone please tell me the best way to write values to a file that I have opened as "write"? The values I need to write are both integer and character strings. Thank you!
    Janice

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It depends on how you plan to read them back. The formatting of a file is very important, as is the portability. These factors determine what functions you use to write to the file and in what order.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    Smile

    Thank you for your reply Prelude. I have the file opened as write only (no append). There are 5 variables whose values I want written to this file. Four of them are declared as integers and the fifth one is declared as a character array of 6. I hope what I just told you helps. Janice

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming you just want to write them to the file like that and the character array is nul terminated then this should work fine:

    fprintf ( file, "%d %d %d %d %s", a, b, c, d, array );

    If the character array is not nul terminated then you will have to specify how many characters to write and make sure that there are no garbage values in unused elements:

    fprintf ( file, "%d %d %d %d ", a, b, c, d );
    for ( i = 0; i < 6; i++ ) putc ( array[i], file );

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest

    Smile

    Thanks Prelude. I will try this.
    Janice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM