C 为什么没有从文本文件打印任何内容?

C 为什么没有从文本文件打印任何内容?,c,C,我在用C语言做一些练习,特别是open()和read()。然而,当我试图打印一个文本文件(text.txt)的内容时,我遇到了一个僵局,这个文本文件与c文件位于同一目录中。将打印以字节为单位的大小,但不会打印文件的内容 我根据文件大小分配了空间,并为空终止符添加了“1”。read()的手动页面声明它将文件内容读入第二个参数(void*buff)。我错过了什么/做错了什么 #include <stdio.h> #include <stdlib.h> #include <

我在用C语言做一些练习,特别是open()和read()。然而,当我试图打印一个文本文件(text.txt)的内容时,我遇到了一个僵局,这个文本文件与c文件位于同一目录中。将打印以字节为单位的大小,但不会打印文件的内容

我根据文件大小分配了空间,并为空终止符添加了“1”。read()的手动页面声明它将文件内容读入第二个参数(void*buff)。我错过了什么/做错了什么

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

int main(int argc, char* argv[]){
    int fd = open("text.txt", O_RDONLY);

    if (fd == -1){
        printf("%s\n", strerror(errno));
        return -1;
    }

    else {
        int func = lseek(fd, 0, SEEK_END);
        printf("%d bytes in size\n", func);
        char* ptr = (char*)malloc(sizeof(char)*func + 1);
        read(fd, ptr, func);
        printf("%s", ptr);
        free(ptr);
    }
    close(fd);
    return 0;
}```
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
int fd=打开(“text.txt”,仅限ordu);
如果(fd==-1){
printf(“%s\n”,strerror(errno));
返回-1;
}
否则{
int func=lseek(fd,0,SEEK_END);
printf(大小为%d字节,func);
char*ptr=(char*)malloc(sizeof(char)*func+1);
读取(fd、ptr、func);
printf(“%s”,ptr);
免费(ptr);
}
关闭(fd);
返回0;
}```

在读取之前,您需要移回文件的开头

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

int main(int argc, char* argv[]){
    int fd = open("text.txt", O_RDONLY);

    if (fd == -1){
        printf("%s\n", strerror(errno));
        return -1;
    }

    else {
        int size = lseek(fd, 0, SEEK_END);
        printf("%d bytes in size\n", size);
        char* ptr = (char *) malloc(sizeof(char) * size + 1);
        
        lseek(fd, 0, SEEK_SET);  // move back to the beginning

        read(fd, ptr, size);
        printf("%.*s", size, ptr);
        free(ptr);
    }
    close(fd);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char* argv[]){
    int fd = open("text.txt", O_RDONLY);

    if (fd == -1){
        printf("%s\n", strerror(errno));
        return -1;
    }

    else {
        int size = lseek(fd, 0, SEEK_END);
        printf("%d bytes in size\n", size);
        char* ptr = (char *) malloc(sizeof(char) * size + 1);
        
        lseek(fd, 0, SEEK_SET);  // move back to the beginning

        read(fd, ptr, size);
        printf("%.*s", size, ptr);
        free(ptr);
    }
    close(fd);
    return 0;
}

将文件指针移回int func=lseek(fd,0,SEEK_END);'之后的起始位置并为空终止符“..”添加了“1”,然后没有插入它:(
(venv) [ttucker@zim stackoverflow]$ cat text.txt 
asdf
qwer
sdfg

(venv) [ttucker@zim stackoverflow]$ gcc -o print print.c 
(venv) [ttucker@zim stackoverflow]$ ./print 
15 bytes in size
asdf
qwer
sdfg