bexita said:
After that I output into another text file but the content is messy. Whats wrong with my input ?
any advise? thx:crying:
Hello, bexita.
Creating file input/output with C++'s file streams (even for binary input/output) is VERY simple and relatively easy!
Here, I've taken the liberty of rewriting your program for you (with slight modifications along the way):
#include <iostream>
#include <fstream>
// You could use std::string instead of char*/char[] here as well
struct STAFF {
char* name;
char* title;
char* position;
STAFF() { }
STAFF(char* name, char* title, char* position) : name(name), title(title), position(position) { }
};
int main() {
// Our single, lonely employee :(
STAFF staff("bexita", "Programmer", "Newbie");
// A binary stream to write output to
std::fstream file;
// Open the file up for binary output (checking for an error state)
file.open("staff.bin", std::ios::out | std::ios::app | std::ios::binary);
if (!file.good()) {
file.close();
std::cerr << "Error opening file! Aborting program...\n";
return 0;
}
// Write our single STAFF structure to the file (checking for an error state)
file.write((char*)&staff, sizeof(STAFF));
if (file.bad()) {
file.close();
std::cerr << "Error writing to file! Aborting program...\n";
return 0;
}
// Close the file, and exit the program! :)
file.close();
return 0;
}
If you're honestly wanting to learn more about binary file input/output with C++, here are some resources to get started with:
-
ostream - C++ Reference (Reference: "Unformatted output")
-
C++ Binary File I/O (Bottom of the page, should jump on its own)
Google's a great resource. Have fun, and good luck (and belated merry Christmas)!! :)