gcc -Wall -W -g3で警告がコンパイルされない< inputfile.c> バイナリは何も表示せず、$ promptも表示されません。 gccバージョン4.9.2(Ubuntu 4.9.2-10ubuntu13)。私は、Keith HavilandによるUnix System Programmingの本から取り上げました。 read()呼び出しの例次のread()コールのコードが停止するのはなぜですか?
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#define BUFSIZE 512
int main()
{
char buffer[BUFSIZE];
int fd;
ssize_t nread;
long total = 0;
if((fd = open("fi.txt", O_RDONLY) == -1)){
printf("error in opening file\n");
exit(1);
}
while((nread = read(fd, buffer, BUFSIZE))>0)
total += nread;
printf("Total characters: %ld\n", total);
exit(0);
}
gdbの表示内容を理解できません。 fi.txtには、Helloという単語が1つだけ含まれています。
:
.
(gdb) s
13 if((fd = open("fi.txt", O_RDONLY) == -1)){
(gdb) s
open() at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) print fd
No symbol "fd" in current context.
(gdb) s
0xb7fdbbd0 in __kernel_vsyscall()
(gdb) s
Single stepping until exit from function __kernel_vsyscall,
which has no line number information.
main() at usp5.c:17
17 while((nread = read(fd, buffer, BUFSIZE))>0)
(gdb) print fd
$1 = 0
(gdb) s
read() at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) s
0xb7fdbbd0 in __kernel_vsyscall()
(gdb) s
Single stepping until exit from function __kernel_vsyscall,
which has no line number information.
s
main() at usp5.c:19
19 printf("Total characters: %ld\n", total);
(gdb) print total
$2 = 0
(gdb) print nread
$3 = 2
(gdb) s
__printf (format=0x804863d "Total characters: %ld\n") at printf.c:28
28 printf.c: No such file or directory.
(gdb) print total
No symbol "total" in current context.
(gdb) s
__x86.get_pc_thunk.bx() at ../sysdeps/i386/i686/multiarch/strcat.S:55
55 ../sysdeps/i386/i686/multiarch/strcat.S: No such file or directory.
(gdb) s
__printf (format=0x804863d "Total characters: %ld\n") at printf.c:32
32 printf.c: No such file or directory.
(gdb) s
33 in printf.c
(gdb) list
28 in printf.c
(gdb) s
_IO_vfprintf_internal (s=0xb7fb4e80 <_IO_2_1_stdout_>, format=0x804863d "Total characters: %ld\n",
ap=0xbfffed84 "") at vfprintf.c:222
222 vfprintf.c: No such file or directory.
(gdb) list
......
同様のメッセージが表示されます。その後、あなたは正しい結果を得るでしょう
if((fd = open("fi.txt", O_RDONLY) == -1)){
if((fd = open("fi.txt", O_RDONLY)) == -1){
へ:
変更このラインコード:
あなたはねあなたの 'open'条件に適切な式のparam setがありません。 [**正確な**この質問の重複](http://stackoverflow.com/questions/13169693/file-descriptors-open-returns-zero) – WhozCraig