2017-05-04 25 views
0

我想将file1内容复制到file2,但在写入命令file2仍然为空之后。write()不写入数据

int fd1; 
int fd2; 
size_t len; 
size_t nbytes; 
ssize_t bytes_read; 

fd1 = open(file1, O_RDWR); 

fd2 = open(file2, O_WRONLY | O_CREAT | O_TRUNC, 0644); 
char *buf[len]; 

nbytes = sizeof(buf); 
bytes_read = read(fd1, buf, nbytes); 

write(fd2, &bytes_read, nbytes); 

close(fd1); 
close(fd2); 

return 0; 

这条线有什么不好吗?

char *buf[len]; 

我应该使用malloc还是memset?

+5

_这里有什么不好的? char * buf [len]; _ yes,你需要一个字符串(一个字符数组),而不是指向字符的指针数组,更改为'char buf [len];',并且您正在使用未初始化的len。 –

+0

'sizeof(buf)'是编译时,'buf'初始化不是。 – tilz0R

+0

那么第一个len是未定义的。此外,通过使用char * buf [len],您可以创建一个“2D”数组[或者至少是一个数组上的指针] – kaldoran

回答

4

你可以试试这个程序:

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

int main(int argc, char* argv[]) 
{ 
    int fd1, fd2; 
    size_t len, nbytes; 
    ssize_t bytes_read; 
    struct stat st; 

    fd1 = open("./file1.txt", O_RDWR); 

    if ((fd1 != -1) &&(fstat(fd1, &st) == 0)) { 
     len = st.st_size; 
     } 

    fd2 = open("./file2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); 

    char buf[len]; 
    nbytes = sizeof(buf); 

    if((fd1 != -1) && (fd2 != -1)){ 
    bytes_read = read(fd1, buf, nbytes); 
    write(fd2, buf, nbytes); 
    close(fd1); 
    close(fd2); 
    }else { 
    printf("error \n"); 
    exit(-1); 
    } 

    return 0; 

} 

你应该从char *buf[len]的BUF改变char buf[len]和获得的文件1,您可以使用这些指令的长度:

fseek(fp, 0L, SEEK_END); 
len = ftell(fp); 

使用BUF之前,尝试使用memset

+1

请解释:'bytes_read = read(fd1,buf,nbytes);写(fd2,buf,nbytes);'你为什么不写入读取的字节数? – ThingyWotsit

+1

什么是memset()?这是无意义的循环浪费。 – ThingyWotsit

+0

此答案无法检查错误!假设每个系统调用都是成功的,这是一个非常糟糕的编程习惯。 – user3629249

1

以下提议代码:

  1. 完全编译
  2. 检查和处理错误
  3. 纠正意见所列问题的问题
  4. 注:这是假定文件名是从命令线参数

和现在的代码

#include <stdio.h> // perror(), printf() 
#include <stdlib.h> // exit(), EXIT_FAILURE 

#include <sys/types.h> // open() 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h>  // write(), close(), read() 

#define MAX_BUF_LEN 1024 

int main(int argc, char *argv[]) 
{ 
    int fd1; 
    int fd2; 

    ssize_t bytes_read; 

    if(3 != argc) 
    { 
     fprintf(stderr, "USAGE: %s inputFileName outputFileName\n", argv[0]); 
     exit(EXIT_FAILURE); 
    } 

    if(0 > (fd1 = open(argv[1], O_RDONLY))) 
    { 
     perror("open for input file failed"); 
     exit(EXIT_FAILURE); 
    } 


    if(0 > (fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644))) 
    { 
     perror("open for output file failed"); 
     close(fd1); // cleanup 
     exit(EXIT_FAILURE); 
    } 


    char buf[ MAX_BUF_LEN ]; 


    while((bytes_read = read(fd1, buf, MAX_BUF_LEN))) 
    { 
     if(0 > bytes_read) 
     { // then read error event 
      perror("read failed"); 
      break; // exit while() loop 
     } 

     else 
     { 
      // EDIT: following line incorrect. 
      //ssize_t bytes_written = write(fd2, &bytes_read, (size_t)bytes_read); 
      // corrected line: 
      ssize_t bytes_written = write(fd2, buf, (size_t)bytes_read); 

      if(bytes_read != bytes_written) 
      { // then write error event 
       fprintf(stderr, "bytes read: %ld, bytes written: %ld\n", bytes_read, bytes_written); 
       break; // exit while() loop 
      } 
     } 
    } // end while() 

    close(fd1); 
    close(fd2); 

    return 0; 
} // end function: main 
+0

您的代码存在一些问题。我尝试将一个单词放在输入文件中,但是我没有将其输入到输出文件中。当我在inputFile中放置一个句子时,我在outputFile中的句子前面找到了一些垃圾。 –

+0

是的,我犯了一个错误。 'write()'语句从变量'bytes_read'的地址开始,而不是在'buf []'的地址。感谢您的支持。 – user3629249