Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Binary files, why does this code crash?

Reply
Thread Tools

Binary files, why does this code crash?

 
 
William Payne
Guest
Posts: n/a
 
      08-02-2004
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: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


 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      08-02-2004
"William Payne" <(E-Mail Removed)> 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:ut);


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

&number

 
Reply With Quote
 
 
 
 
William Payne
Guest
Posts: n/a
 
      08-02-2004

"Tim Love" <(E-Mail Removed)> wrote in message
news:celh3o$3ft$(E-Mail Removed)...
> "William Payne" <(E-Mail Removed)> 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: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments