2015-10-22 66 views
0

我目前正在用C寫一個聊天程序。它在兩個終端中運行,並使用一個程序。會話1的樣品輸入將如下所示:c中基於回合的聊天程序

./a.out a.txt b.txt Phil 

哪裏A.TXT是用於輸入/讀取的文件,並且是b.txt將用於輸出的文件/發送消息。菲爾是聊天手柄。 由此可見,會話2的輸入的樣子:

./a.out b.txt a.txt Jill 

,使得輸入文件是第一屆會議的輸出文件,讓聊天的工作,併爲兩個終端相互交談。

這個計劃看起來像的運行示例:

Send: Are you there? 
Received [Jill]: Yup! 
Send: Okay see ya! 
Received [Jill]: Bye! 
^C 

反之亦然中的其他終端。但是,我無法讓我的程序發送文件並自動接收它們。我的輸出看起來是正確的,但是如果我發送一條消息,我只能收到一條消息,這種情況會影響聊天的目的。我的問題是,在我的while循環中我錯了什麼地方,在我能夠讀取其他會話發送的消息之前,必須發送消息到哪裏?以下是我目前使用的代碼:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, const char *argv[]) { 

    // Initialize file variables 
    FILE *in; 
    FILE *out; 

    // Initialize incoming, incoming check, and outgoing variables 
    char inc[300] = " "; 
    char incCheck[300] = " "; 
    char send[300]; 
    char handle[300]; 
    int size, counter; 
    counter=1; 
    // This checks if a user has already entered a message 
    // i.e. entered the chat 
    if (in = fopen(argv[1], "r")) { 
     fclose(in); 
    } 
    else { 
     // create an empty in.txt to bypass segfault if the file doesn't already exist 
     in = fopen(argv[1], "w"); 
     fclose(in); 

    // To check if anything has been received yet. 
     if (strcmp(inc, incCheck) == 0) { 
     printf("Nothing has been received yet.\n"); 
     } 
    } 
    // The while loop that reads and writes the files 
    while(1) { 

     // Read the incoming file and put it to a string 
     // It will read the incoming file over and over until a message is seen 
     in = fopen(argv[1], "r"); 
     fgets(inc, size, in); 
     fclose(in); 

     // This counter is only for the first message, since it is not possible 
     // that one has already been received. 
    if (counter > 0) { 
     size=sizeof(send); 
     printf("Send: \t"); 
     fgets(send, size, stdin); 
     out = fopen(argv[2], "w"); 
     fprintf(out, "%s",send); 
     fclose(out); 
     counter=0; 
    } 

     // If the incoming file is different, print it out 
    if (strcmp(inc, incCheck) != 0) { 
     printf("Received [%s]: %s", argv[3], inc); 

     in = fopen(argv[1], "r"); 
     fgets(inc, size, in); 
     strcpy(incCheck, inc); 
     fclose(in); 
     // And prompt to send another message. 
     size=sizeof(send); 
     printf("Send: \t"); 
     fgets(send, size, stdin); 
     out = fopen(argv[2], "w"); 
     fprintf(out, "%s",send); 
     fclose(out); 

     in = fopen(argv[1], "r"); 
     fgets(inc, size, in); 
     fclose(in); 

     } 
    } 
+1

你最後一次發現有人通過終端聊天?這是一項家庭作業嗎?什麼*確切*是老師的任務? –

+0

這是作業。我們需要編寫一個實現基於回合的聊天系統的C程序。 「聊天用戶交互 作爲終端I/O出現,數據作爲純文本文件在聊天過程之間傳遞。只有純文本文件 I/O可用於傳輸聊天消息,您不能使用網絡或其他系統調用「。 –

回答

0

將聊天的一側設置爲啓動器可解決問題。這足夠嗎?在下面的代碼中,通過將其中一個手柄設置爲「s」來解決此問題。

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, const char *argv[]) { 

    // Initialize file variables 
    FILE *in; 
    FILE *out; 

    // Initialize incoming, incoming check, and outgoing variables 
    char inc[300] = " "; 
    char incCheck[300] = " "; 
    char send[300]; 
    char oldsend[300]; 
    char handle[300]; 
    int size, counter; 
    counter=1; 
    // This checks if a user has already entered a message 
    // i.e. entered the chat 
    if (in = fopen(argv[1], "r")) { 
      fclose(in); 
    } 
    else { 
      // create an empty in.txt to bypass segfault if the file doesn't already exist 
      in = fopen(argv[1], "w"); 
      fclose(in); 

    // To check if anything has been received yet. 
      if (strcmp(inc, incCheck) == 0) { 
      printf("Nothing has been received yet.\n"); 
      } 
    } 
     in = fopen(argv[1], "r"); 
      fgets(incCheck, size, in); 
      fclose(in); 
    in = fopen(argv[2], "r"); 
      fgets(oldsend, size, in); 
      fclose(in); 

    // The while loop that reads and writes the files 
    while(1) { 

      // Read the incoming file and put it to a string 
      // It will read the incoming file over and over until a message is seen 

      // This counter is only for the first message, since it is not possible 
      // that one has already been received. 
    if ((counter > 0) && (strcmp(argv[3],"s")==0)) { 
     size=sizeof(send); 
     do { 
    printf("Send: \t"); 
    fgets(send, size, stdin); 
     }while (strcmp(send, oldsend)==0); 
     strcpy (oldsend,send); 
     out = fopen(argv[2], "w"); 
     fprintf(out, "%s",send); 
     fclose(out); 
     counter=0; 
} 
in = fopen(argv[1], "r"); 
      fgets(inc, size, in); 
      fclose(in); 
} 

      // If the incoming file is different, print it out 
    if (strcmp(inc, incCheck) != 0) { 
printf("Received [%s]: %s", argv[3], inc); 

      in = fopen(argv[1], "r"); 
      fgets(inc, size, in); 
      strcpy(incCheck, inc); 
      fclose(in); 
      // And prompt to send another message. 
      size=sizeof(send); 
do { 
      printf("Answer: \t"); 
      fgets(send, size, stdin); 
} while (strcmp(send, oldsend)==0); 
      strcpy (oldsend,send); 
      out = fopen(argv[2], "w"); 
      fprintf(out, "%s",send); 
      fclose(out); 
      } 
    } 
    return 0; 
}