2016-01-18 33 views
1

我在我的代碼有問題的過程代碼:隊列Linux無法寫

FILE *fp; 
int  i, 
    counter; // Liczba liter w wiadomosci 
wchar_t buffer[1024], *line; 
struct MsgStructure WB; // Write Buffor 
setlocale(LC_ALL, "pl_PL.UTF-8"); 

while(run) 
{ 
    fp = fopen(FIFO, "r"); 
    fwide(fp, 1); 
    while ((line = fgetws(buffer, sizeof buffer/sizeof buffer[0], fp)) != NULL) 
    { 
      counter = 0; 
      for (i = 0; line[i] != L'\0'; i++) 
       if (iswalpha(line[i])) 
       counter++; 

      WB.Msg = counter; 

      if ((WriteToQueue(qid, &WB)) == -1)  
      { 
       perror("Error\n"); 
      } 

    } 

    fclose(fp); 
} 

我的程序從FIFO讀取文件再算上字母的金額,然後我想寫它來排隊,但我得到我不能寫排隊的錯誤,因爲「錯誤的參數」

我的結構:

struct MsgStructure { 
    long int MsgType; 
    int Msg; 
}; 

WriteToQueue是一個簡單的函數:

int WriteToQueue(int qid, struct MsgStructure *qbuf){ 
    int result, BufSize; 

    BufSize = sizeof(struct MsgStructure) - sizeof(long);   
    result = msgsnd(qid, qbuf, BufSize, 0); 
     return(result); 
} 

我的消息類型爲int和計數器數據類型爲int了。我不知道爲什麼這不起作用。也許這是setlocale的問題? 隊列正在其他進程中創建。

+0

更大的「錯誤的參數」你的意思是EINVAL(又名無效參數)? – hroptatyr

+0

「無效參數」 –

+0

您是否成功使用'msgget()'獲得'qid'? – hroptatyr

回答

3

正如在評論中討論:

EINVAL Invalid msqid value, or nonpositive mtype value, 
      or invalid msgsz value (less than 0 or greater than 
      the system value MSGMAX). 

,前者已被排除,而後者顯然是不正確的,即BufSize是在示例代碼sizeof(int)

mtype<=0。從手冊頁:

The msgp argument is a pointer to caller-defined 
    structure of the following general form: 

     struct msgbuf { 
      long mtype;  /* message type, must be > 0 */ 
      char mtext[1]; /* message data */ 
     }; 

這是根據對msgrcv()msgtyp說法,如果爲0,有着特殊的意義。

解決辦法是設置在struct MsgStructureMsgType到的東西比0

+0

它幫助了很多:) –

+0

額外的問題:這個程序是用於計數字母,但是當我有行像:qwertqq \ 00 azerty它只計數到7,但應該13.我怎樣才能解決這個問題? –

+0

在這種情況下不要使用'fgetws',自己讀取緩衝區(使用'read()'),使用'memchr()'檢查換行符,這允許在你的字符串中有'\ 0's,然後從緩衝區開始處理塊,直到memchr()給你的內容爲止,設置緩衝區超過換行符並重新開始。總結:做手動線分塊 – hroptatyr