2016-12-28 96 views
1

我的程序(如下)寫(與PWRITE())文本PWRITE()寫入到文件,並讀取(與PREAD())從文件。我的問題是pread函數不能從文件中讀取我的文本,關閉函數(程序的最後一部分)有什麼問題?結果在第二部分。我的錯誤在哪裏?閱讀與PREAD(),並在C

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

    int main() 
    { 

     int fd, nr, nr2, nw, nw2; 
     char fl_nm[]={"file_io/pwrite.txt"}; 
     char buf_wr[]={"hello everyone this is first text\n"}; 
     char buf_wr2[]={"Did you miss me? Don't afraid\n"}; 
     char buf_rd[120]; 
     char buf_rd2[120]; 

     //open file 
     fd = open(fl_nm, O_RDWR|O_CREAT, 0777); 
     nw = pwrite(fd, &buf_wr, strlen(buf_wr), 14); 

     //error checking 
     if(fd == -1){ 
      perror("[error in open]\n"); 
     } 
     else if(nw == -1){ 
      perror("[error in write]\n"); 
     } 
     else{ 

      /*if open and write process are okey, read first write data 
      * from file*/ 
      nr = read(fd, &buf_rd, sizeof(buf_rd)); 

      //display succeeded message about first write and open process 
      printf("[file is opened]\n"); 
      printf("[succeeded write(1) process]\n"); 

      //read process error control 
      if(nr == -1){ 
       perror("[error in read]\n"); 
      } else{ 
      printf("[reading(1) data] from %s\n", fl_nm); 
      printf("[%s]\n", buf_rd); 
      } 

     } 

     //second write process. 
     nw2= pwrite(fd, &buf_wr2, strlen(buf_wr2), 30); 

     //write error checking 
     if(nw2 == -1){ 
      perror("[error in write 2]\n"); 
     }else{ 

      /*if write process is correct 
      * second read process*/ 
      nr2 = read(fd, &buf_rd2, sizeof(buf_rd)); 

      printf("-----------------------------------\n"); 
      printf("[succeeded write(2) process]\n"); 
      printf("[reading(2) data] from %s\n", fl_nm); 
      printf("[%s]\n", buf_rd2); 
     } 

     //close file 
     close(fd); 

     //error checking for close process 
     if(close(fd) == -1){ 
      perror("[error in close]\n"); 
     }else{ 
      printf("[succeeded in close]\n"); 
     } 

     return 0; 
    } 

結果:

$ gcc pwrite.c -o pwrite 
$ ./pwrite 
[file is opened] 
[succeeded write(1) process] 
[reading(1) data] from file_io/pwrite.txt 
[] 
----------------------------------- 
[succeeded write(2) process] 
[reading(2) data] from file_io/pwrite.txt 
[] 
[error in close] 
: Bad file descriptor 
+0

每[標準](http://pubs.opengroup.org/onlinepubs/9699919799/functions/read:

相反,使用一個循環打印每個字節(),'pread()','write()'和'pwrite()'返回'ssize_t',而不是'int'。 –

回答

2

1)close()因爲你關閉文件兩次失敗:

//close file 
close(fd); 

//error check close process 
if(close(fd) == -1){ 

第一次調用close(fd);後,fd變得不確定,並close(fd)第二個呼叫失敗。您只需要將第一個電話去掉close(fd);即可。

2)要打印buf_rd,就好像它是一個C-字符串。 read()不會用空字節終止buf_rd

3)你在隨機偏移(14和30)使用pwrite()寫入。但是read()從當前偏移量讀取 - 這意味着起始字節可能爲空字節,因此%s立即停止打印(即,不打印任何內容)。你閱讀的東西比你寫的更多。這意味着read()將返回小於請求的字節數。因此,使用返回值read()來獲取成功讀取的字節數。

for (size_t l = 0; l < nr; l++) 
    printf("%c", buf_rd[l]); 

for (size_t l = 0; l < nr2; l++) 
    printf("%c", buf_rd2[l]); 
1

您使用指針錯誤,訪問將數組的地址應該是用他們的名字,而不是&名。

取代& buf_wr與buf_wr,訪問到不正確的地址與& buf_wr編寫會有損壞你的籌碼,也變量裏面堆定義

編輯:

更換

nw = pwrite(fd, &buf_wr, strlen(buf_wr), 14); 

nw = pwrite(fd, buf_wr, strlen(buf_wr), 14); 

和所有其他實例..

+0

nw = pwrite(fd,buf_wr,strlen(buf_wr),14);依此類推...... –

+0

輸出沒有變化。它仍然是同樣的結果。 – metarose

+0

你替換了所有實例嗎?編輯你的文章,並把編輯代碼 –