#include // so I can do reasonable i/o #include // open at least #include // open #include // open #include // read #include // strcmp #include // perror using namespace std; void SeekAndTell(int fd); void MyWrite(int fd); void MyRead(int fd); void DumpFile(int fd); int main() { int theFD, openFlags; mode_t filePerms; openFlags = O_CREAT | O_RDWR | O_TRUNC; filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; theFD = open("SeekFile", openFlags, filePerms); if (theFD == -1){ perror("Could not open the output file "); return 1; } // initiall fill the file with some data. char c = 'A'; int i; for(i=0;i<10;i++) { write(theFD, &c, sizeof(char)); c = c + 1; } char action; do { cout << " r - read, w - write, d - dump, q - quit" << endl; cout << "Enter your action (r,w,d,q) -> "; cin >> action; switch (action) { case 'r': MyRead(theFD); break; case 'w': MyWrite(theFD); break; case 'd': DumpFile(theFD); break; } } while (action != 'q'); close(theFD); return 0; } void SeekAndTell(int fd) { off_t toPos, len; len = lseek(fd, 0, SEEK_END); cout << "The file is " << len << " bytes long" << endl; cout << endl; do { cout << "Enter a position to seek to => "; cin >> toPos; } while (toPos >= len or toPos < 0); cout << "\tSeeking to " << toPos << " byte" << endl; lseek(fd, toPos, SEEK_SET); return; } void MyRead(int fd) { char letter; SeekAndTell(fd); read(fd, &letter, sizeof(char)); cout <<"\t" << letter << " is at that position" << endl; return; } void MyWrite(int fd) { char letter; SeekAndTell(fd); cout << "Enter a letter to save there => "; cin >> letter; write(fd, &letter, sizeof(char)); return ; } void DumpFile(int fd){ off_t len; int i; char c; len = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); for(i=0;i