fgets()的问题
Ashnu 2004-05-05 08:23:08 1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #define MAXLINE 82
6 main()
7 {
8 char buff[82];
9 char usrname[12]="0", passwd[24]="0";
10 int fd=open("tmpfile", O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
11 printf("username: ");
12 scanf("%12s", usrname);
13 printf("password: ");
14 scanf("%24s", passwd);
15 printf("Subject: ");
16 if (fgets(buff, MAXLINE, stdin)==NULL)
17 {
18 perror("fgets");
19 exit(1);
20 }
21 if (buff[79] != '\n') buff[80]='\n';
22 printf("buff: %s\n", buff);
23 write(fd, "Subject: ", strlen("Subject: "));
24 write(fd, buff, strlen(buff));
25 printf("Content:\n");
26 while (fgets(buff, MAXLINE, stdin) != NULL)
27 write(fd, buff, strlen(buff));
28 close(fd);
29 }
代码如上.问题是,为什么第16行的fgets()没有起作用呢?应该怎么解决?
PS: 标准I/O库要想搞明白还真是不容易.每次都在它上面吃亏.这次恐怕又是缓存惹的祸 :(