昨天用C语言写了个程序,老是报错,程序是这样的
2 #include < unistd.h >
3 #include < sys/types.h >
4 #include < fcntl.h >
5 #include < errno.h >
6 #include < sys/stat.h >
7 #include < stdlib.h >
9 int main(int argc, char* argv[])
10 {
11 int fd;
12 off_t where;
13 char c;
15 if ((fd=open("tt",O_RDONLY)==-1))
16 {
17 printf("sth wrong while opening file!\n");
18 exit(-1);
19 }
25 if ((where = lseek(fd, 1, SEEK_CUR)==-1))
26 {
27 perror("lseek failed!");
28 exit(-1);
29 }
30 printf("current pos is %ld\n",where);
31 exit(0);
32 }
编译好运行的时候输出从25行的分支出去了,老是“lseek failed!”顺便声明,文件是存在的哦,用gdb调试,15行的fd打开后为0,就是stdin,看来看去,才发现是==比=的优先级高,而我的括号又打错了地方,实际上是15行和25行都写错了,正确的写法是:
15 if ((fd=open("tt",O_RDONLY))==-1)
25 if ((where = lseek(fd, 1, SEEK_CUR))==-1)
c语言总是认为你是正确的,而往往你不是这样 :)
0 comments:
Post a Comment