C 使用系统调用(打开、读取、写入)显示文件内容

C 使用系统调用(打开、读取、写入)显示文件内容,c,system-calls,cat,C,System Calls,Cat,我试图实现一个名为displaycontent的命令,该命令将文本文件名作为参数并显示其内容。我将在Linux中使用open(),read(),write(),和close()系统调用来实现这一点。它的作用应该有点像UNIXcat命令,用于显示文件内容 以下是我到目前为止的情况: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #i

我试图实现一个名为displaycontent的命令,该命令将文本文件名作为参数并显示其内容。我将在Linux中使用
open()
read()
write()
,和
close()
系统调用来实现这一点。它的作用应该有点像UNIX
cat
命令,用于显示文件内容

以下是我到目前为止的情况:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    char content[fd];   

    errno = 0;
    fd = open(argv[1], O_RDONLY);


    if(fd < 0)
    {
        printf("File could not be opened.\n");
        perror("open");
        return 1;
    }
    else 
    {

        read(fd, content, sizeof(content)-1);
        write(1, content, sizeof(content)-1);
    }

return 0;
}

文件内容后面有奇怪的符号和东西。我不确定出了什么问题,任何帮助都将不胜感激。谢谢。

fd
未初始化,因此未确定
内容的大小

无论如何,您不应该为此使用fd。如果这只是一个练习,您可以使用一个较大的固定数字。否则,您需要获取文件大小并使用它

要获取文件长度,可以按照以下示例进行操作:

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

int main()
{
    int fd = open( "testfile.txt", O_RDONLY );
    if ( fd < 0 )
        return 1;

    off_t fileLength = lseek( fd, 0, SEEK_END );  // goes to end of file
    if ( fileLength < 0 )
        return 1;

    //  Use lseek() again (with SEEK_SET) to go to beginning for read() call to follow.
    close( fd );
    return 0;
}
#包括
#包括
#包括
int main()
{
int fd=open(“testfile.txt”,仅限ordu);
如果(fd<0)
返回1;
off_t fileLength=lseek(fd,0,SEEK_END);//转到文件末尾
if(文件长度<0)
返回1;
//再次使用lseek()(设置SEEK_)转到read()调用的开头。
关闭(fd);
返回0;
}

(我今天没有编译这个,我只是从内存中读取。如果有拼写错误,它们应该是次要的)

fd
没有初始化,因此
内容的大小也没有确定

无论如何,您不应该为此使用fd。如果这只是一个练习,您可以使用一个较大的固定数字。否则,您需要获取文件大小并使用它

要获取文件长度,可以按照以下示例进行操作:

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

int main()
{
    int fd = open( "testfile.txt", O_RDONLY );
    if ( fd < 0 )
        return 1;

    off_t fileLength = lseek( fd, 0, SEEK_END );  // goes to end of file
    if ( fileLength < 0 )
        return 1;

    //  Use lseek() again (with SEEK_SET) to go to beginning for read() call to follow.
    close( fd );
    return 0;
}
#包括
#包括
#包括
int main()
{
int fd=open(“testfile.txt”,仅限ordu);
如果(fd<0)
返回1;
off_t fileLength=lseek(fd,0,SEEK_END);//转到文件末尾
if(文件长度<0)
返回1;
//再次使用lseek()(设置SEEK_)转到read()调用的开头。
关闭(fd);
返回0;
}
(我今天没有编译,我只是凭记忆。如果有拼写错误,应该是次要的)

使用
bytes=read(fd,content,sizeof(content)-1)以捕获号码
读取的字节数。然后在
写入中使用字节(1,内容,字节)仅写入
已读取的字节用户3121023

使用
bytes=read(fd,content,sizeof(content)-1)以捕获号码
读取的字节数。然后在
写入中使用字节(1,内容,字节)仅写入
已读取的字节用户3121023


char内容[fd]什么?@devbrs,看起来你忘了使用
close()
@donjuedo:程序终止时文件句柄会自动关闭。谢谢你user3121023@EOF,没错,但他确实说过他的任务是使用所有4个函数。
charcontent[fd]什么?@devbrs,看起来你忘了使用
close()
@donjuedo:程序终止时文件句柄会自动关闭。谢谢你user3121023@EOF,没错,但他确实说过他的任务是使用所有4个函数。即使这是固定的,也可能仍然有垃圾输出。
write
输出整个缓冲区,而不仅仅是读取的内容。你说得对。正如您所评论的,我正在添加我的答案。:-)即使这是固定的,仍有可能是垃圾输出。
write
输出整个缓冲区,而不仅仅是读取的内容。你说得对。正如您所评论的,我正在添加我的答案。:-)而
bytes
应为
ssize\u t
类型。而
bytes
应为
ssize\u t
类型。