2017-09-22 75 views
0

我正在实施ftp,我想上传和下载文件,当我下载或上传PDF文件时,它们已损坏。如何处理读取任何文件,使用read()write()mmap?下面是我尝试过的简化代码。读取和写入pdf或二进制数据在C

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

int  is_regular_file(const char *path) 
{ 
    struct stat path_stat; 

    stat(path, &path_stat); 
    return (S_ISREG(path_stat.st_mode)); 
} 

int  ft_get_file_size(const char *filename) 
{ 
    struct stat file; 
    int   fd; 

    if (!is_regular_file(filename)) 
     return (-1); 
    fd = open(filename, O_RDONLY); 
    memset(&file, 0, sizeof(struct stat)); 
    fstat(fd, &file); 
    close(fd); 
    return (file.st_size); 
} 

char *read_file(const char *filename) 
{ 
    char *content; 
    int  file_size; 
    int  fd; 
    ssize_t retval; 

    if ((file_size = ft_get_file_size(filename)) <= 0) 
     return (NULL); 
    content = (char *)malloc(sizeof(char) * file_size + 1); 
    fd = open(filename, O_RDONLY); 
    retval = read(fd, content, file_size); 
    content[retval + 1] = '\0'; 
    close(fd); 
    return (content); 
} 

void write_file(char *file, char *content) 
{ 
    int fd; 

    fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); 
    if (fd) 
     write(fd, content, strlen(content)); 
    close(fd); 
} 

int main() { 
    char *test = read_file("ftp.en.pdf"); 
    write_file("copy.pdf", test); 
    return EXIT_SUCCESS; 
} 

下载和上传文件时,从文件读取的所有数据,然后将数据发送到插座的过程。我试过使用mmap,我仍然收到损坏的文件。

文件已损坏的错误消息

Corrupted file

+0

有书面几个问题你的代码。 'read()'和'write()'返回'ssize_t',而不是'int'。 'struct stat'的'st_size'元素是'off_t',也不是'int'。 'fd = open(...);如果(fd)...'也是错误的。 open()在失败时返回“-1”,计算结果为true。你也不要检查'read()'和'write()'的返回值,以确保调用实际读或写你请求的每个字节。 –

+0

通常,当调用'read()'或'write()'时,需要在循环中调用它们,使用数据位置的滑动窗口,直到读取或写入所有数据。调用'malloc()'时调用 – user3629249

+0

,1)返回的类型是'void *',它可以被分配给任何指针。铸造只是混淆了代码,使其更难以理解,调试等。建议删除演员阵容。 2)表达式'sizeof(char)'在C标准中定义为1.将任何东西乘以1都不起作用。建议删除该表达式。 3)总是检查(!= NULL)返回的值以确保操作成功。在main()中使用 – user3629249

回答

4

为二进制数据可以有\0字符,你不能把你的内容作为一个字符串,所以strlen(content)是错误的。您必须返回您的read_file函数的内容大小。

例如,将您的函数定义为char *read_file(const char *filename, int *size)并返回大小*size。同样地定义你写的功能void write_file(char *file, char *content, int size)

(并忘记了的malloc +1)