我有這些它寫在C++函數:如何解決C++ char轉換?
#include ...
char read_somthing()
{
int n = 0, spot = 0;
char buf = '\0';
char response[13];
memset(response, '\0', sizeof response);
do {
n = read(fd, &buf, 1);
sprintf(&response[spot], "%c", buf);
spot += n;
} while(buf != '\r' && n > 0);
cout << "Response: " << response << endl;
return buf;
}
void main()
{
cout << read_somthing();
char response = read_somthing();
cout << response;
}
的問題是,首先COUT作品和打印的真正價值,但第二個將返回空行,我認爲這是與第二在主要功能線,我如何解決它?
PS:我註釋掉了cout << read_somthing();
,但輸出中沒有任何內容。
顯示read_somthing()正在做什麼。它如何填充「響應」? – trojanfoe
用你提供的東西看起來不錯......也許<<你的cout結束時的endl可能會使它更具可讀性?或者也許你的read_something壞了?如果你只是從read_something返回'*',它是否工作? – mark
你知道你正在從你的'read_something()'函數返回一個字符而不是一些字符串嗎? –