簡體   English   中英

C中基於回合的聊天程序

[英]Turn based chat program in c

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

./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);

        }
    }

將聊天的一側設置為發起者可以解決此問題。 夠了嗎? 在下面的代碼中,可以通過將句柄之一設置為“ 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM