I made a program that writes a structure into a binary file, and then reads that binary file into a different variable. Writing into file and reading from it works fine, however the program crashes on exit.  I tried using the same variable for reading which works, but is not the solution I'm looking for. I want to know why the program crashes when I read structure from binary file into a different variable.  Here's the code:  #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;ctime&gt; #include &lt;stdlib.h&gt;  using namespace std;  struct t_student{     string name;     string lastname;     int mark; };  int main(){     t_student student[30], student_read[30];     int n, i;     srand(time(0));      fstream dat("student.dat", ios::in | ios::out | ios::binary | ios::trunc);      cout &lt;&lt; "Input number of students (up to 30): ";     cin &gt;&gt; n;     if(n &gt; 30) n = 30;     cout &lt;&lt; endl;      cin.ignore();     for(i = 0; i &lt; n; i++){         cout &lt;&lt; "Input first name: ";         getline(cin, student[i].name);          cout &lt;&lt; "Input last name: ";         getline(cin, student[i].lastname);          student[i].mark = rand()%5+1;          cout &lt;&lt; endl;          dat.write((char *) &amp;student[i], sizeof(student[i]));     }      dat.clear();     dat.seekg(0);     i = 0;      while(!dat.eof()){         dat.read((char *) &amp;student_read[i], sizeof(student_read[i]));         if(dat.eof()) break;         cout &lt;&lt; endl &lt;&lt; student_read[i].name &lt;&lt; " " &lt;&lt; student_read[i].lastname &lt;&lt; "  -  " &lt;&lt; student_read[i].mark;         i++;     }     dat.close();      cin.get();     //exit(0); }   As you might notice, I also added exit(0) function, which prevents the program from crashing, but I want to know why the program crashes in the first place.  Thanks for your help.
124187
I have a small hierarchy of objects that I need to serialize and transmit via a socket connection.  I need to both serialize the object, then deserialize it based on what type it is.  Is there an easy way to do this in C++ (as there is in Java)?  Are there any C++ serialization online code samples or tutorials?  EDIT: Just to be clear, I'm looking for methods on converting an object into an array of bytes, then back into an object.  I can handle the socket transmission.
87608
I'm getting these errors while linking, both messages have to do with the same object file.  CALL16 reloc at 0x5f8 not against global symbol   and  could not read symbols: Bad value   The 2nd message seems to be the reason I'm getting the CALL16 error, but the file compiles just fine.  Any tips on fixing this?  FYI, I'm cross compiling for a MIPS target and using gcc 4.1.2  EDIT: No luck so far:  Here are my flags used: -fPIC,-Wl,-rpath,-Wl,-O1  I've also tried the following without success: -mno-explicit-relocs  -mexplicit-relocs  -mlong-calls  -mno-long-calls  -mxgot  -mno-xgot    Meanwhile, I'll go back to the source at this point and investigate more.
11442
I have an application that writes information to file. This information is used post-execution to determine pass/failure/correctness of the application. I'd like to be able to read the file as it is being written so that I can do these pass/failure/correctness checks in real time.  I assume it is possible to do this, but what are the gotcha's involved when using Java? If the reading catches up to the writing, will it just wait for more writes up until the file is closed, or will the read throw an exception at this point? If the latter, what do I do then?   My intuition is currently pushing me towards BufferedStreams. Is this the way to go?
19556
Yes,  but there must be a canonical or most efficient or most concise way. I'll add answers I know of and see what percolates to the top.  To be clear, the question is how best to read the contents of a file into a string. One solution per answer.
91148
I need to compare two binary files and get the output in the form:  &lt;fileoffset-hex> &lt;file1-byte-hex> &lt;file2-byte-hex>  for every different byte. So if file1.bin is    00 90 00 11   in binary form and file2.bin is    00 91 00 10   I want to get something like    00000001 90 91   00000003 11 10   Is there a way to do this in Linux? I know about cmp -l but it uses a decimal system for offsets and octal for bytes which I would like to avoid.
8383
Again me with vectors. I hope I'm not too annoying. I have a struct like this :  struct monster  {     DWORD id;     int x;     int y;     int distance;     int HP; };   So I created a vector :  std::vector&lt;monster&gt; monsters;   But now I don't know how to search through the vector. I want to find an ID of the monster inside the vector.   DWORD monster = 0xFFFAAA; it = std::find(bot.monsters.begin(), bot.monsters.end(), currentMonster);   But obviously it doesn't work. I want to iterate only through the .id element of the struct, and I don't know how to do that. Help is greatly appreciated. Thanks !
114370
This is about files straight from the compiler, say g++, and the -o (outfile) flag.  If they are binary, shouldn't they just be a bunch of 0's and 1's?  When you cat them, you get unintelligible output but also intact words.  If you file them, you get the answer immediately - there seem to be no computation. Do the binary files in fact have headers with this kind of information?  I thought a binary executable was just the program just compiled, only in the form of machine instructions that your CPU can instantly and unambiguously understand. If so, isn't that instruction set just bit patterns? But then, what's all the other stuff in the binaries? How do you display the bits?  Also, if you somehow get hold of the manual of your processor, could you write a binary manually, one machine instruction at a time? That would be terribly ineffective, but very fascinating if you got it to work even for a "Hello World!" demo.
27265
I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.   Can anyone help me?
13020
I want to initialize a struct element, split in declaration and initialization. This is what I have:  typedef struct MY_TYPE {   bool flag;   short int value;   double stuff; } MY_TYPE;  void function(void) {   MY_TYPE a;   ...   a = { true, 15, 0.123 } }   Is this the way to declare and initialize a local variable of MY_TYPE in accordance with C programming language standards (C89, C90, C99, C11, etc.)? Or is there anything better or at least working?  Update I ended up having a static initialization element where I set every subelement according to my needs.
82601
I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
115337
I have read several places that the difference between c_str() and data() (in STL and other implementations) is that c_str() is always null terminated while data() is not. As far as I have seen in actual implementations, they either do the same or data() calls c_str().  What am I missing here? Which one is more correct to use in which scenarios?
21332
What is the simplest way (least error-prone, least lines of code, however you want to interpret it) to open a file in C and read its contents into a string (char*, char[], whatever)?
924
The following code receives seg fault on line 2:  char *str = "string"; str[0] = 'z';  // could be also written as *str = 'z' printf("%s\n", str);   While this works perfectly well:  char str[] = "string"; str[0] = 'z'; printf("%s\n", str);   Tested with MSVC and GCC.
32342
Is there a (cross-platform) way to get a C FILE* handle from a C++ std::fstream ?  The reason I ask is because my C++ library accepts fstreams and in one particular function I'd like to use a C library that accepts a FILE*.
50127
What would be the most efficient method of reading a text file into a dynamic one-dimensional array? reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it?
3852
Is a string literal in c++ created in static memory and destroyed only when the program exits?