Writing variables to a text doc?

This is a discussion on Writing variables to a text doc? within the C++ Programming forums, part of the General Programming Boards category; So if i want to write a regular string to a text doc, i would do this... Code: TFileStream *FStream; ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Question Writing variables to a text doc?

    So if i want to write a regular string to a text doc, i would do this...

    Code:
    TFileStream *FStream;
    FStream = new TFileStream("tester.txt",fmOpenWrite);
    FStream->Write("hello",5);
    That works fine and all, the word 'hello' is printed into the document. But i want to be able to write the value of a variable into a text document, and when i try this...

    Code:
    int x=20;
    TFileStream *FStream;
    FStream = new TFileStream("tester.txt",fmOpenWrite);
    FStream->Write(x,5);
    ...it writes some weird symbols to the document, instead of 20. i'm using borland c++ builder 5 btw. anyone have any tips/suggestions/advice/etc?


    Things i've tried already...
    - changing the second arguement of Write() to different numbers other than 5, still writes the same thing
    - searching the help file in my IDE for functions similar to Write() that may do what i want, but none of them worked either

    Thanks~~~

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Although this is Delphi, the answer is pretty simple.
    public function TStream.Write(
    const Buffer;
    Count: LongInt
    ):LongInt; virtual; abstract;

    Write attempts to write Count bytes from Buffer to the stream. It returns the actual number of bytes written to the stream.
    My guess is that Write treats the int given from you as a simple sequence of bytes, and writes
    those bytes in their binary form to your file.
    It is probably wisest to use IntToStr to convert your value, and then WriteAnsiStr to write it.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, write will write the BINARY representation of the data, in this case mostly zero's. It will also (most likely) write a random garbage byte, as you are specifying a length of 5, when an integer is (most often) 4 bytes, so a fifth byte of whatever is after your int will also be written.

    Binary files are fine for some things, but obviously, you will need text formatting to be able to read it as a human [unless you are a bit more nerdy and can make do with a hex display of the file].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Also aren't Delphi/Pascal pointers supposed to look something like:
    Code:
    TFileStream: ^FStream;
    The whole snippets feel wrong to me... although i haven't seen that oracle language for
    some years... i could be wrong.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Writing to Text File
    By slowcoder in forum C Programming
    Replies: 2
    Last Post: 08-22-2007, 12:19 PM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  3. Small HTML question
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 12-28-2003, 11:37 PM
  4. Read variables and stuff from a text file
    By ChrisJ in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 11:06 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21