if (p[0] == '#') //if(in.eof()) { char reply[] = "抱歉,这个问题我不知道,您可以为我解答,或等待人工服务"; int k = send(*clientSock, reply, sizeof(reply), 0); int receByt = recv(*clientSock, recvbuf, sizeof(recvbuf), 0); if (receByt > 0 && (strcmp(recvbuf, "可以") == 0 || strcmp(recvbuf, "好的") == 0)) { char reply2[] = "请输入你的答案:"; k = send(*clientSock, reply2, sizeof(reply2), 0); if (k < 0) { cout << "发送失败!" << endl; } receByt = recv(*clientSock, recvbuf, sizeof(recvbuf), 0); if (receByt > 0) { out << "Q:" << recvbuf << endl;//写入文件 } out << "A:" << recvbuf << endl; } in.clear(); streampos sp = in.tellg(); in.seekg(-sp, ios::cur); break; } 修改以上代码,使客户所问的问题能写入txt里
时间: 2024-02-10 21:12:24 浏览: 66
eof.rar_PCA/EOF_eof_eof程序_pca
可以这样修改代码:
```c++
if (p[0] == '#') {
// ...
if (receByt > 0 && (strcmp(recvbuf, "可以") == 0 || strcmp(recvbuf, "好的") == 0)) {
char reply2[] = "请输入你的答案:";
k = send(*clientSock, reply2, sizeof(reply2), 0);
if (k < 0) {
cout << "发送失败!" << endl;
}
receByt = recv(*clientSock, recvbuf, sizeof(recvbuf), 0);
if (receByt > 0) {
out << "Q:" << recvbuf << endl; // 写入文件
}
char reply3[] = "谢谢您为我解答问题!";
k = send(*clientSock, reply3, sizeof(reply3), 0);
if (k < 0) {
cout << "发送失败!" << endl;
}
}
in.clear();
streampos sp = in.tellg();
in.seekg(-sp, ios::cur);
out.close(); // 关闭文件
break;
}
```
这样,当客户回答“可以”或“好的”时,程序会提示客户输入答案,并将客户输入的问题写入文件中。同时,程序会发送一条感谢客户的消息。最后,程序会关闭文件,以确保写入数据已经保存。
阅读全文