我正在写 3200 个 float ,如下所示:
float fft[3200] = {0};
get_fft_frame(dev, fft, 3200); // rf sensor
int written = write(sd, fft, sizeof(fft));
然后像这样阅读:
float *fft = new float[3200];
int inRead = 0;
while (inRead < (3200*4)) {
//sleep(1);
int bytesRead = read(sd, fft + inRead, (3200*4) - inRead);
inRead += bytesRead;
}
所有数据都在 1 次写入中写入,但需要两次读取调用才能获取所有数据。第一次读取返回 11584,第二次返回 1216。
问题是第二个读取调用只读取零。但是,如果我取消注释 sleep 调用,它会读取所有正确的数据。
有没有更好的方法来不 sleep 地获取数据?
最佳答案
你混淆了你的偏移量。 inRead
以字节为单位,但当您将其添加到 fft
时,它会根据指针算术规则乘以 sizeof float
。 read()
调用中的偏移表达式应该是
((char*)fft)+inRead
关于c++ - c socket多次调用read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38757853/