源码:
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 int main(void)
8 {
9 printf("Enter the file's name:\n");
10 char name[20];
11 scanf("%s", name);
12 int fd = open(name, O_RDWR);
13 if(fd == -1){
14 perror(name);
15 return -1;
16 }
17 char buf[10];
18 int len = 0;
19 while((len = read(fd, buf, 10)) != 0){
20 if(len == -1){
21 perror("read");
22 break;
23 }
24 if(len < 10){
25 buf[len] = '\n';
26 printf("%s", buf);
27 break;
28 }
29 printf(" %s", buf);
30 }
31 printf("\n");
32 close(fd);
33 return 0;
34 }
用编译的可执行文件读取这个源文件
./bin/read
src/read.c
输出的结果是:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void)
{
printf("Enter the file's name:\n");
char name[20];
scanf("%s", name);
int fd = open(name, O_RDWR);
if(fd == -1){
perror(name);
return -1;
}
char buf[10];
int len = 0;
while((len = read(fd, buf, 10)) != 0){
if(len == -1){
perror("read");
break;
}
if(len < 10){
buf[len] = '\n';
printf(" %s", buf);
break;
}
printf("%s", buf);
}
printf("\n");
close(fd);
return 0 ;
}
urn 0
红色部分是多余的,为什会出现这种情况?
read函数出错,大家帮我看看错在哪里了啊
-
- 帖子: 16
- 注册时间: 2010-12-24 13:50
-
- 帖子: 13
- 注册时间: 2009-12-02 21:47
Re: read函数出错,大家帮我看看错在哪里了啊
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 int main(void)
8 {
9 printf("Enter the file's name:\n");
10 char name[20];
11 scanf("%s", name);
12 int fd = open(name, O_RDWR);
13 if(fd == -1){
14 perror(name);
15 return -1;
16 }
17 char buf[10];
18 int len = 0;
19 while((len = read(fd, buf, 10)) != 0){
20 if(len == -1){
21 perror("read");
22 break;
23 }
24 if(len < 10){
25 buf[len] = '\n';
26 printf("%s", buf);
27 break;
28 }
29 printf(" %s", buf);
memset(buf,0,sizeof(buf));
30 }
31 printf("\n");
32 close(fd);
33 return 0;
34 }
在29行下面,加了一句代码,自己运行过,已经通过。 楼主加个朋友,我也在学习,QQ:371641065
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 int main(void)
8 {
9 printf("Enter the file's name:\n");
10 char name[20];
11 scanf("%s", name);
12 int fd = open(name, O_RDWR);
13 if(fd == -1){
14 perror(name);
15 return -1;
16 }
17 char buf[10];
18 int len = 0;
19 while((len = read(fd, buf, 10)) != 0){
20 if(len == -1){
21 perror("read");
22 break;
23 }
24 if(len < 10){
25 buf[len] = '\n';
26 printf("%s", buf);
27 break;
28 }
29 printf(" %s", buf);
memset(buf,0,sizeof(buf));
30 }
31 printf("\n");
32 close(fd);
33 return 0;
34 }
在29行下面,加了一句代码,自己运行过,已经通过。 楼主加个朋友,我也在学习,QQ:371641065
-
- 帖子: 16
- 注册时间: 2010-12-24 13:50
Re: read函数出错,大家帮我看看错在哪里了啊
法比斯 写了:1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6
7 int main(void)
8 {
9 printf("Enter the file's name:\n");
10 char name[20];
11 scanf("%s", name);
12 int fd = open(name, O_RDWR);
13 if(fd == -1){
14 perror(name);
15 return -1;
16 }
17 char buf[10];
18 int len = 0;
19 while((len = read(fd, buf, 10)) != 0){
20 if(len == -1){
21 perror("read");
22 break;
23 }
24 if(len < 10){
25 buf[len] = '\n';
26 printf("%s", buf);
27 break;
28 }
29 printf(" %s", buf);
memset(buf,0,sizeof(buf));
30 }
31 printf("\n");
32 close(fd);
33 return 0;
34 }
在29行下面,加了一句代码,自己运行过,已经通过。 楼主加个朋友,我也在学习,QQ:371641065
先谢谢了~
运行的很好,我是加错字符串结束符了,25行应该是:buf[len] = '\0';这样就可以了~~~
大家共同进步~~