// // // // // // // // // // // // // // // // // // // // // // // // // // CS270 Class Example // // Author: Robert Heckendorn // University of Idaho // #include #include #include #include #include void usage(char *myname) { fprintf(stderr, "Usage: %s loc amount file\n", myname); } int main(int argc, char **argv) { unsigned long long int loc; // location in file to read from unsigned long long int amount; // number of bytes to read char *file; // name of file char *value; // location of buffer to read into int valueInt; // space allocated to read an int into int valueLen; // amount actually read int fd; // file descriptor if (argc!=4) { usage(argv[0]); exit(0); } // get args without error checking loc = atoi(argv[1]); amount = atoi(argv[2]); file = strdup(argv[3]); // open a file with the system call open. It is opened for reading and writing // this assumes the file exists and will not create it if it is not if ((fd = open(file, O_RDWR))==-1) { printf("ERROR: count not open %s\n", file); usage(argv[0]); exit(1); } // TASK 1: read the requested number of bytes from the location in the given file // allocate up some space to read into value = (char *)malloc(amount); // move the read/write "head" to the requested location lseek(fd, loc, SEEK_SET); // try reading the requested bytes if ((valueLen = read(fd, value, amount))==-1) { printf("ERROR: reading %s at location %lld\n", file, loc); exit(1); } // if valueLen==0 then an immediate EOF was found if (valueLen==0) { printf("WARNING: trying to read past the end of the file. No bytes read.\n", file); } // dump the bypes read as hex for (int i=loc; i