fwrite的用法, 想必大家肯定很熟悉了。 但是, 很多人在调用fopen后, 不定返回的fp进行非空判断, 恕我直言, 确实有点小流氓。 实际上,在linux开发中, 受权限影响, 很多时候调用fopen会失败, 返回NULL. 在本文中, 我们来看看, 万一产生了core, 该如下定位。
实际上, 对于gdb调试core, 我们已经比较熟悉了, 现在来强化一下, 如下:
[taoge@localhost test]$ ls
test.c
[taoge@localhost test]$ cat test.c -n
1 #include <stdio.h>
2
3 int main()
4 {
5 FILE *fp = NULL;
6 int a = 1;
7 int b = 2;
8 int c = a + b;
9
10 fwrite("abc", sizeof("abc"), 1, fp);
11
12 return 0;
13 }
[taoge@localhost test]$ gcc -g test.c
[taoge@localhost test]$ ls
a.out test.c
[taoge@localhost test]$ ./a.out
Segmentation fault (core dumped)
[taoge@localhost test]$ ls
a.out core.3355 test.c
[taoge&#