After doing some reading on FILE data type I decided to practice, I came up with this little program that reads any part of any file:
I'd like some suggestions, critics, tips on how to make my code better.Code:#include <iostream> #include <string> #include <stdio.h> using namespace std; int main() { FILE * file; char * file_name = new char[255]; int read_from, lenght, file_size; char * output; cout << "What file do you want to be read? "; cin >> file_name; if(!(file = fopen(file_name, "r+"))) { cout << "Could not open file."; return 0; } fseek(file, 0, SEEK_END); file_size = ftell(file); rewind(file); cout << "File " << file_name << "(" << file_size << "bytes) sucessfully opened.\n\n"; cout << "Read from offset: "; cin >> read_from; fseek(file, read_from, SEEK_SET); if(read_from > file_size) { cout << "Given file does not reach that far!\n"; return 0; } cout << "How many bytes to be read? "; cin >> lenght; if((read_from + lenght) > file_size) { cout << "Given file does not reach that far!\n"; return 0; } output = new char[lenght]; for(int x = 0; x < lenght; x++) { output[x] = getc(file); } cout << "Output: " << output; return 0; }



LinkBack URL
About LinkBacks



