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:: out);
file.write(name , sizeof(name));
file.write((cha r*)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((cha r*)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 2 1651
"William Payne" <mi************ **@student.liu. se> 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:: out);
file.write(name , sizeof(name)); file.write((cha r*)number, sizeof(unsigned short));
&number
"Tim Love" <tp*@eng.cam.ac .uk> wrote in message
news:ce******** **@pegasus.csx. cam.ac.uk... "William Payne" <mi************ **@student.liu. se> 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:: out); file.write(name , sizeof(name)); file.write((cha r*)number, sizeof(unsigned short)); &number
Doh, what a stupid mistake! Thanks alot for spotting it, Tim!
/ WP This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wwj |
last post by:
void main()
{
char* p="Hello";
printf("%s",p);
*p='w';
printf("%s",p);
}
|
by: Arnold |
last post by:
I need to read a binary file and store it into a buffer in memory (system
has large amount of RAM, 2GB+) then pass it to a function. The function
accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512
words to it at a time. So I would pass them in chunks of 512 words until the
whole file has been processed. I haven't worked with binary files before so
I'm confused with how to store the binary file into memory. What sort of...
|
by: knapak |
last post by:
Hello
I'm a self instructed amateur attempting to read a huge file from disk... so
bear with me please... I just learned that reading a file in binary is
faster than text. So I wrote the following code that compiles OK. It runs and
shows the requested output. However, after execution, it pops one of those
windows to send error reports online to the porgram creator. I have managed
to find where the error is but can't see what's wrong....
|
by: zeecanvas |
last post by:
Hi,
First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.
I'm maintaining a big program. I moved all (program-wide scope) global
variables outside of the files they were defined it, and created some
files that just hold global variables definitions (just variables,
without any function definition). So, depending on the purpose/category
of variables, they're defined...
|
by: Me Alone |
last post by:
Hello:
I am trying to edit some C code I found in "The definitive guide to
using, programming, and administering MySQL" by Paul DuBois. This C
client program connects and then segfaults when the function load_image
is called. Would anyone be able to point me to what I might be doing
wrong?
Thanks in advance,
C Newbie
| |
by: Ludo |
last post by:
Hi,
I'm working on an open source project and I have a problem with a
binary file. When I read the binary file with the program downloaded
from sourceforge, I have no problem. However, I'm unable to read this
binary file with the program compiled with Microsoft Visual C++
Express Edition SP1. The program read wrong values from the file and
crash. I don't understand because:
*the program downloaded from sourceforge was compiled with...
|
by: zl2k |
last post by:
hi, there
I have a appendable binary file of complex data structure named
data.bin created by myself. It is written in the following format:
number of Data, Data array
Suppose I have following data.bin (3 Data appended to 2 Data):
2, data0, data1, 3, data0, data1, data2
|
by: osama178 |
last post by:
Hi,
What does it mean for an object to be binary compatible? And why
aren't STL objects binary compatible? Any insights, links, resources
for further reading are greatly appreciated.
Thanks.
|
by: chifos0 |
last post by:
Can any one tell me how to open .exe files in binary so i can edit the in vb. I know you "can't" really edit it, but i am trying to make a program that randomly mutates an .exe file's binary code. i want just need to know how you open it so my program can do what i designed it to do (randomly alter some of the binary code) and how to save the binary back into the similar .exe format. lol i can almost imagine my mutated exe files, im sure most of...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |