我目前正在用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);
}
}
答案 0 :(得分: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;
}