如何获取指向缓冲区的指针以读取内容并检查剩余数据 [英] How to get pointer to buffer to read the contents and check the remaining data
问题描述
大家好,
我正在用c ++做一个应用程序,我需要使用指针来读取缓冲区的内容。我使用以下代码声明指向缓冲区的指针。
Hi All,
I am doing an application in c++ in which i need to use pointer to read the contents of the buffer. I used the following code to declare pointer to the buffer.
unsigned char *ptrToBuf = gchOBDReplyBuf;
缓冲区包含来自文本文件的数据。我的问题是如何使用指针读取此缓冲区的内容?每次我需要检查缓冲区剩余内容,因为我需要在完全读取缓冲区数据时退出。请帮助,因为我不熟悉指针的内容..
先谢谢
The buffer has the data from a text file in it. My question is how can i read the contents of this buffer using pointers? Also each time i need to check the buffer remaining contents as I need to exit when the buffer data have been fully read.Please help as I am not familiar with the contents of pointers..
Thanks in Advance
推荐答案
试试这个......让我知道。
try this...and let me know.
if (gchOBDReplyBuf) {
// get length of file:
gchOBDReplyBuf.seekg (0, gchOBDReplyBuf.end);
int length = gchOBDReplyBuf.tellg();
gchOBDReplyBuf.seekg (0, gchOBDReplyBuf.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
gchOBDReplyBuf.read (buffer,length);
if (gchOBDReplyBuf)
std::cout <<"all characters read successfully";
else
std::cout<< "error: only << gchOBDReplyBuf.gcount()<< could be read;
gchOBDReplyBuf.close();
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
如果您收到任何错误,请退回。 ...
if u getting any error please revert back....
试试这个..
Try this..
#include <iostream.h>
main() {
unsigned char gchOBDReplyBuf[]= "abcdefghijklmnopqrstuvwxyz";
unsigned char *ptrToBuf = gchOBDReplyBuf;
for ( int index = 0; index < 26; index++ ) {
cout << char(*ptrToBuf+index);
}
cout << endl;
}
这篇关于如何获取指向缓冲区的指针以读取内容并检查剩余数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!