#include #include #include #include #include #include #include #include using namespace std; int main() { char buffer[] = "XXXXXX.tmp"; char buffer2[] = "GONE_XXXXXX"; int fd; int i; long tmp; string junk; srand(time(NULL)); cout << "Creating a temp file." << endl; fd = mkstemps(buffer, 4); if (-1 == fd) { perror("mkstemps"); return(-1); } cout << "opened " << fd << " and the name is " << buffer << endl; cout << "This one will still be around after exit " << endl; write(fd, buffer, strlen(buffer)); close(fd); fd = mkstemp(buffer2); if (-1 == fd) { perror("mkstemp"); return(-1); } cout << "Created a new file called " << buffer2 << endl; cout << "Press enter to continue " << endl; getline(cin, junk); unlink(buffer2); cout << "unlinked it " << endl; cout << "Writing 10 random numbers " << endl; for(i=0;i<10;i++) { tmp = rand() % 100; cout << tmp << " "; write(fd, &tmp, sizeof(tmp)); } cout << endl; cout << "Press enter to continue " << endl; getline(cin, junk); cout << "Seeking to the beginning of the file " << endl; lseek(fd, 0, SEEK_SET); for(i=0;i<10;i++) { read(fd, &tmp, sizeof(tmp)); cout << tmp << " "; } cout << endl; close (fd); return 0; }