mix mode file reading/writing?

Discussion in 'C++' started by john smith, Jul 29, 2003.

  1. john smith

    john smith Guest

    Hi, I have a file format that is going to contain some parts in ascii, and
    some parts with raw binary data. Should I open this file with ios::bin or
    no?

    For example:
    filename: a.bin
    number of points = 123
    @@@begin data@@@
    gibberish follows.....

    @@@end data@@@

    I will use the >> and << operators for the ascii parts, and iostream's read
    and write functions for the binary parts. Any advice or suggestions will be
    appreciated. Thanks.

    Smith
     
    john smith, Jul 29, 2003
    #1
    1. Advertising

  2. john smith

    Greg P. Guest

    "john smith" <> wrote in message
    news:bg5c3p$1fbf$...
    > Hi, I have a file format that is going to contain some parts in ascii, and
    > some parts with raw binary data. Should I open this file with ios::bin or
    > no?

    The only difference between text and binary reads are the lack of \r\n reads
    for binary. binary reads consider the characters, which mean end of line for
    text, potential values themselves. I would suggest opening the file in
    binary mode (for most of your file-related coding).
    --

    Regards,
    Greg P.

    Golden Rule of Open Source Programming:
    "Don't whine about something unless you plan to implement it yourself"
     
    Greg P., Jul 29, 2003
    #2
    1. Advertising

  3. john smith

    ES Kim Guest

    "Greg P." <> wrote in message
    news:fTqVa.1622$...
    > "john smith" <> wrote in message
    > news:bg5c3p$1fbf$...
    > > Hi, I have a file format that is going to contain some parts in ascii, and
    > > some parts with raw binary data. Should I open this file with ios::bin or
    > > no?

    > The only difference between text and binary reads are the lack of \r\n reads
    > for binary. binary reads consider the characters, which mean end of line for
    > text, potential values themselves. I would suggest opening the file in
    > binary mode (for most of your file-related coding).
    > --
    >
    > Regards,
    > Greg P.
    >
    > Golden Rule of Open Source Programming:
    > "Don't whine about something unless you plan to implement it yourself"
    >
    >


    In addition, if you read a binary file in text mode, your program may
    quit reading prematurely since it cannot detect the correct EOF position.

    --
    ES Kim
     
    ES Kim, Jul 29, 2003
    #3
  4. john smith

    john smith Guest

    Hello, I still need some help with this.

    When I open the file for writing in ASCII, it writes fine. Then I close it
    and reopen it like so:
    in.open(filename, ios_base::binary | ios_base:ate), and then I write my
    data. When I open the file in notepad, the contents have become all
    gibberish, like it's a binary file. But what I'm trying to do is something
    like some Linux installation scripts. There is a text area which is human
    readable, then it's followed by the binary area. The presumably the text
    readable area is parsed by the shell and the binary area is executed. While
    that's not what I'm trying to do, I am trying to write and read some raw
    data, while being able to parse information about that data in raw text...

    Any insights would be appreciated. Thanks in advance.

    Smith

    "ES Kim" <> wrote in message
    news:bg5d3j$5ti$...
    > "Greg P." <> wrote in message
    > news:fTqVa.1622$...
    > > "john smith" <> wrote in message
    > > news:bg5c3p$1fbf$...
    > > > Hi, I have a file format that is going to contain some parts in ascii,

    and
    > > > some parts with raw binary data. Should I open this file with

    ios::bin or
    > > > no?

    > > The only difference between text and binary reads are the lack of \r\n

    reads
    > > for binary. binary reads consider the characters, which mean end of line

    for
    > > text, potential values themselves. I would suggest opening the file in
    > > binary mode (for most of your file-related coding).
    > > --
    > >
    > > Regards,
    > > Greg P.
    > >
    > > Golden Rule of Open Source Programming:
    > > "Don't whine about something unless you plan to implement it yourself"
    > >
    > >

    >
    > In addition, if you read a binary file in text mode, your program may
    > quit reading prematurely since it cannot detect the correct EOF position.
    >
    > --
    > ES Kim
    >
    >
     
    john smith, Jul 30, 2003
    #4
  5. john smith wrote:

    > Hello, I still need some help with this.
    >
    > When I open the file for writing in ASCII, it writes fine. Then I close it
    > and reopen it like so:
    > in.open(filename, ios_base::binary | ios_base:ate), and then I write my
    > data. When I open the file in notepad, the contents have become all
    > gibberish, like it's a binary file. But what I'm trying to do is something
    > like some Linux installation scripts. There is a text area which is human
    > readable, then it's followed by the binary area. The presumably the text
    > readable area is parsed by the shell and the binary area is executed. While
    > that's not what I'm trying to do, I am trying to write and read some raw
    > data, while being able to parse information about that data in raw text...
    >
    > Any insights would be appreciated. Thanks in advance.
    >
    > Smith


    Please post your code and indicate where you are having problems.
    Also state the expected behavior and the actual behavior.
    This will allow people to assist you better.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jul 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1cmF2aQ==?=

    Problem while reading excel file with mix datatypes

    =?Utf-8?B?UGF1cmF2aQ==?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    604
    Alvin Bruney [MVP]
    Mar 4, 2004
  2. John J Lee
    Replies:
    3
    Views:
    552
    bruno at modulix
    Dec 1, 2005
  3. Edward Loper
    Replies:
    0
    Views:
    527
    Edward Loper
    Aug 7, 2007
  4. John J Lee
    Replies:
    0
    Views:
    560
    John J Lee
    Aug 7, 2007
  5. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    476
    Edward Loper
    Aug 9, 2007
Loading...

Share This Page