Binary files, why does this code crash?

Discussion in 'C++' started by William Payne, Aug 2, 2004.

  1. Hello! Consider the following code:

    #include <fstream>
    #include <iostream>

    int main()
    {
    char name[256] = {"testing"};
    unsigned short number = 4711;

    std::fstream file("foo.dat", std::ios_base::binary | std::ios_base::eek:ut);

    file.write(name, sizeof(name));
    file.write((char*)number, sizeof(unsigned short));

    file.close();

    file.open("foo.dat", std::ios_base::binary | std::ios_base::in);

    char name2[256];
    unsigned short number2 = 0;

    file.read(name2, sizeof(name2));
    file.read((char*)&number2, sizeof(unsigned short));

    file.close();

    std::cout << "name2 = " << name2 << std::endl;
    std::cout << "number2 = " << number2 << std::endl;
    }

    This program ends with a segmentation fault when I run it. It seems to
    create the file foo.dat, but it appears to be completely empty. No output is
    shown on the screen. What am I doing wrong here?

    Oh, and one more thing. My compiler issues the following warning about this
    line:
    file.write((char*)number, sizeof(unsigned short));
    binary_file.cpp:12: warning: cast to pointer from integer of different size

    Is it safe to ignore this warning or is it part of the problem with the
    code? When I program I like to crank up the warning level of the compiler to
    the max and fix all warnings so I'd like to get rid of it even if it's
    harmless in this case.

    Thanks for any replies

    / WP
     
    William Payne, Aug 2, 2004
    #1
    1. Advertising

  2. William Payne

    Tim Love Guest

    "William Payne" <> writes:

    >Hello! Consider the following code:


    >#include <fstream>
    >#include <iostream>


    >int main()
    >{
    > char name[256] = {"testing"};
    > unsigned short number = 4711;


    > std::fstream file("foo.dat", std::ios_base::binary | std::ios_base::eek:ut);


    > file.write(name, sizeof(name));
    > file.write((char*)number, sizeof(unsigned short));

    &number
     
    Tim Love, Aug 2, 2004
    #2
    1. Advertising

  3. "Tim Love" <> wrote in message
    news:celh3o$3ft$...
    > "William Payne" <> writes:
    >
    > >Hello! Consider the following code:

    >
    > >#include <fstream>
    > >#include <iostream>

    >
    > >int main()
    > >{
    > > char name[256] = {"testing"};
    > > unsigned short number = 4711;

    >
    > > std::fstream file("foo.dat", std::ios_base::binary |

    std::ios_base::eek:ut);
    >
    > > file.write(name, sizeof(name));
    > > file.write((char*)number, sizeof(unsigned short));

    > &number
    >


    Doh, what a stupid mistake! Thanks alot for spotting it, Tim!

    / WP
     
    William Payne, Aug 2, 2004
    #3
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    11
    Views:
    480
    Michael
    Nov 18, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    609
    PerlFAQ Server
    Feb 11, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    601
    PerlFAQ Server
    Mar 9, 2011
Loading...

Share This Page