gpt4 book ai didi

c - C语言回合制聊天程序

转载 作者:行者123 更新时间:2023-11-30 17:09:27 25 4
gpt4 key购买 nike

我目前正在用 C 语言编写一个聊天程序。它在两个终端上运行,并使用一个程序。 session 1 的示例输入如下所示:

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

其中a.txt是用于输入/读取的文件,b.txt是用于输出/发送消息的文件。 Phil 是聊天 handle 。由此可见, session 2 的输入如下所示:

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

这样输入文件就是第一个 session 的输出文件,使聊天能够正常工作,并使两个终端能够相互对话。

该程序的示例运行如下所示:

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

在另一个终端中反之亦然。但是,我无法让我的程序自动发送和接收文件。我的输出看起来是正确的,但如果我发送一条消息,我只能收到一条消息,这违背了聊天的目的。我的问题是,我的 while 循环中哪里出了问题,我必须先发送一条消息才能读取其他 session 发送的消息?这是我到目前为止的代码:

#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;
}

关于c - C语言回合制聊天程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com