// rewind example // source: http://www.cplusplus.com/ #include #include int main() { int n; FILE *pFile; // 26 chars for the alphabet + NUL character ('\0') to terminate the string // checkout http://www.asciitable.com/ for assic table -- NUL is `\0` is 0 // BTW, we use NUL (one L) to refer to terminating a string, and NULL for // pointers -- NULL pointer. char buffer[27]; pFile = fopen("myfile.txt", "w+"); for (n = 'A'; n <= 'Z'; n++) fputc(n, pFile); rewind(pFile); fread(buffer, 1, 26, pFile); fclose(pFile); // note, once the data is read into a char array (string) // a NULL is added to the end to terminate the string. // try commenting out the line below and see what happens // Result: it might work or get segfault. buffer[26]='\0'; // Create a bug and uncomment the line below. Maybe the result // is a segfault of the puts() runs on until it finds a 0. // try and work it out. // buffer[26]='a'; puts(buffer); return 0; }