当前位置: 技术问答>linux和unix
这个程序读文件怎么不好使?
来源: 互联网 发布时间:2015-12-12
本文导语: #include #include #include #include int main() { char file[12]; read(0, file, 12); int fd_src; char content[512]; if((fd_src = open(file, O_RDWR)) != -1) while(read(fd_src, content, 512)==512) write(1, (const void *)&con...
#include
#include
#include
#include
int main()
{
char file[12];
read(0, file, 12);
int fd_src;
char content[512];
if((fd_src = open(file, O_RDWR)) != -1)
while(read(fd_src, content, 512)==512)
write(1, (const void *)&content, 512);
else
write(2, "Write Error!", 12);
close(fd_src);
exit(0);
}
//屏幕打印Write Error! 读的文件的权限没有问题, 文件名也在正常范围内.
#include
#include
#include
int main()
{
char file[12];
read(0, file, 12);
int fd_src;
char content[512];
if((fd_src = open(file, O_RDWR)) != -1)
while(read(fd_src, content, 512)==512)
write(1, (const void *)&content, 512);
else
write(2, "Write Error!", 12);
close(fd_src);
exit(0);
}
//屏幕打印Write Error! 读的文件的权限没有问题, 文件名也在正常范围内.
|
修改后:
#include
#include
#include
#include
int main()
{
char file[12];
//read(0, file, 12);
scanf("%s", file);
int fd_src;
char content[512];
memset (content, 0, sizeof(content));
if((fd_src = open(file, O_RDWR)) != -1)
while(read(fd_src, content, 512)>0)
write(1, (const void *)&content, 512);
else
write(2, "Write Error!", 12);
close(fd_src);
exit(0);
}
#include
#include
#include
#include
int main()
{
char file[12];
//read(0, file, 12);
scanf("%s", file);
int fd_src;
char content[512];
memset (content, 0, sizeof(content));
if((fd_src = open(file, O_RDWR)) != -1)
while(read(fd_src, content, 512)>0)
write(1, (const void *)&content, 512);
else
write(2, "Write Error!", 12);
close(fd_src);
exit(0);
}