#include // so I can do reasonable i/o #include // open at least #include // open #include // open #include // read #include // strcmp #include // perror const int BUFFER_SIZE = 1024; using namespace std; int main(int argc, char * argv[]) { int inputFD, outputFD, openFlags; mode_t filePerms; int anInt = 5; float aFloat = 3.14159; char letter = 'c'; string aWord ="Hello World"; ssize_t numWritten; char buf[BUFFER_SIZE]; // open the input file. openFlags = O_CREAT | O_WRONLY | O_TRUNC; // rw-rw-rw- filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; outputFD = open("datafile.dat", openFlags, filePerms); if (outputFD == -1){ perror("Could not open the output file "); return 1; } numWritten = write(outputFD, &anInt, sizeof(int)); numWritten = write(outputFD, &aFloat, sizeof(float)); numWritten = write(outputFD, &letter, sizeof(char)); // add 1 to the length to get the null. numWritten = write(outputFD, aWord.c_str(), sizeof(char)*(aWord.size()+1)); if (close(outputFD) == -1) { perror("Fail on closing output file"); return 1; } return 0; }