Test Ftell function when found error, first paste the source code
File name:ftell.c #include <stdio.h> #include <stdlib.h> int main (int argc, char** argv) { file* fp = fopen ("myfile.in", "R"); if (fp = = NULL) { perror ("fopen error"); Exit (1); } Char buf[4]; Fgets (BUF, 4, FP); if (fputs (buf, fp) = = EOF) { perror ("fputs error"); Exit (1); } if (ferror (FP)) { perror ("ferror"); Exit (1); } return 0;}
The error message is as follows
Then debug with GDB, set breakpoints at fputs, output character array
All of a sudden I want to see the value of each character, so I see this
Ah, suddenly remembered that the type of BUF is not char*, although if as a function input parameters will be treated as char*, but the actual type of BUF is char (*) [4]
So the output is 4 char (*) [4], which is the 16 characters that BUF starts
But I'm using P &buf[0] @sizeof (BUF) will error only the values in memory can is extended with ' @ '
Think of the possible need for type conversion, add (char*) after the error, originally because only the value of the dog with the @ extension, GDB will get the value of the pointer, and then move forward with @
So several debugs are as follows
(GDB) P buf$1 = "Lin" (gdb) P &buf[0]$2 = 0x7fffffffde90 "Lin" (GDB) p &buf[0]@4only values in memory can be Extende D with ' @ '. (GDB) p (char*) &buf[0]@4only values in memory can is extended with ' @ '. (GDB) p * (char*) &buf[0]@4$3 = "Lin" (gdb) p *[email protected]$4 = "Lin"
It is not visible unless you view the character of that bit individually
(GDB) p (int) buf[3]$5 = 0 (gdb) p buf[3]$6 = 0 ' \000 '
OK, continue to solve the problem of fputs error.
In fact, Perror displays the information is perfect, the error is bad file descriptor, where the file descriptor is hidden in the file* point of the object, the error is the FP.
Here I'm going to output the information to the screen, so fputs's second input parameter should be the standard output stdout instead of the file pointer fp I opened.
Unable to write because fopen has selected read mode.
Try to change the second parameter of fopen to "r+", allowing the write, the result is as follows
Line 01 Before the text is now changed to Linlin1 because the offset is 3 (that is, the position of ' e ') when reading 3 characters, and then 3 characters are written, so "Lin" replaces "E 0".
It can also be found here that using "R" instead of "r+" to prohibit writing can check for errors that are easily overlooked.
Modified as follows
File name:ftell.c #include <stdio.h> #include <stdlib.h> int main (int argc, char** argv) { c3/>file* fp = fopen ("myfile.in", "R"); if (fp = = NULL) { perror ("fopen error"); Exit (1); } Char buf[4]; Fgets (BUF, 4, FP); if (fputs (buf, stdout) = = EOF) { perror ("fputs error"); Exit (1); } printf ("\nfile offset:%ld\n", Ftell (FP)); if (ferror (FP)) { perror ("ferror"); Exit (1); } Fclose (FP); return 0;} /* Output:linfile offset:3 */
The representation of pointers and array differences when GDB debugs character arrays