2012-04-22 33 views
0

這是一個關於管道和過程的小型C程序,Father過程將創建2個子進程,第一個將讀取鏈中的數字,第二個將讀取這些字母。我開始尋求WORD,我沒有添加保護,這只是一個測試,所以讓我們說約20個字母,然後父進程將編號在第一個管道,並在第二個管道中的字母,然後他將創建一個孩子使用fork(),如果他是孩子,他會從第一個管道讀取數字,如果他是父親,那麼他將創建另一個孩子,讀取這些字母。WRITE和READ有什麼錯誤?

# include <stdio.h> 
# include <unistd.h> 
# include <string.h> 
# include <fcntl.h> 

main() 
{ 
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n"); 
    char * word; 
    printf("please type the word: \n"); 
    scanf("%s",word); 
    printf("Now 2 pipes will be created\n"); 
    int fd1[2]; 
    int fd2[2]; 
    pipe(fd1); pipe(fd2); 
    printf("Now the father will write numbers in the first pipe, and letters in the second\n"); 
    int i; 
    char numbers[20]; int j=0; 
    char caracters[20]; int k=0; 
    for (i=0;i<20;i++) 
    { 
     if(word[i]>='0' && word[i]<='9') //if number 
     { 
      close(fd1[0]); //closing reading 
      write(fd1[1],word[i],2); 

     } 
     else 
     { 
      close(fd2[0]); 
      write(fd2[1],word[i],2); 
     } 

    } 
    printf("The father has wrote in the 2 pipes, now its time for the sons\n"); 
    int f=fork(); 
    if(f==0) //first son 
    { 
     for(i=0;i<20;i++) {   
      close(fd1[1]); //closing writing 
      read(fd1[0],numbers[j],strlen(numbers[j])+1); 
      j++; 

     } 
     printf("first son read everything, he got %d Numbers\n", j); 
    } 
    else 
    { 
     f=fork(); 
     if(f==0) 
     { 
      for(i=0;i<20;i++) {   
      close(fd2[1]); //closing writing 
      read(fd2[0],caracters[k],strlen(caracters[k])+1); 
      k++; 

     } 
     printf("second son read everything, he got %d caracters\n", j); 
    } 
} 

編譯後:

In function 'main': 
Line 25: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 31: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 41: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 41: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 54: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 54: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 60: error: expected declaration or statement at end of input 

回答

2

writeread的原型是

ssize_t write(int fd, const void *buf, size_t count); 

ssize_t read(int fd, void *buf, size_t count); 

參數2至write/read應該是一個指針。但是,您發送是一個字符(這實際上是一個整數)word[i]numbers[i]

同樣的問題,即使你strlen

此外,聲明word作爲數組,而不是隻是一個指針。否則,您將寫入指針所指向的任意位置。或者如果你想保持它作爲一個指針,malloc它的一些記憶。

這一切後,剛及格的wordnumbers代替numbers[j]words[i]你的功能,這都在抱怨

編輯:另外你上次for聲明for(i=0;i<20;i++)缺少一個右括號,因此Line 60: error: expected declaration or statement at end of input錯誤

+0

我用read(fd2 [0],&caracters [k],1); < - 我對其他人做了同樣的事情,添加&,沒有更多的錯誤,但我只得到了1.錯誤:預期聲明或輸入結束時的聲明 – 2012-04-22 19:10:40

+0

@AliBassam檢查我的編輯 – 2012-04-22 19:13:59

+0

固定。但執行後,它要求的話,我鍵入它,然後它顯示2 printfs(現在父親會寫....)然後沒有別的..... – 2012-04-22 19:17:55

0

相反作者:

write(fd1[1],word[i],2); 

這樣做:

write(fd1[1],(void*)&word[i],2); 

......即,將POINTER傳遞到數據的位置,而不是數據本身的值。

+0

我用read(fd2 [0],&caracters [k],1); < - 我對其他人做了同樣的事情,增加&,沒有更多的錯誤,但我得到了只有1. 錯誤:預期聲明或輸入末尾的聲明 – 2012-04-22 19:09:40

+0

什麼行號?它對應什麼? – 2012-04-22 19:11:52

+0

它需要一個固定的右括號。但執行後,它要求單詞,我鍵入它,然後顯示2個printfs(現在父親會寫....)然後沒有別的..... – 2012-04-22 19:17:46

相關問題