Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted

try this...and let me know.

C#
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....
 
Share this answer
 
Comments
nv3 12-Nov-13 3:12am    
You are assuming that gchODBReplyBuf is an istream. But from the question of OP it looks like it is an array of char.
Try this..
C++
#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;
}
 
Share this answer
 
v2
Comments
Lekshmi KR 13-Nov-13 1:50am    
Ok Its working. But now i have to pass this pointer (ptrToBuf) to another buffer(gchOBDResponseBuf) in another class which is c++ class.I did it in following way and caused error:

CMainFrame *myframe;
myframe=(CMainFrame *)AfxGetApp()->m_pMainWnd;
CECUSimulatorView *myview;
myview=(CECUSimulatorView *)myframe->GetActiveView();

gchOBDResponseBuf = myview->ptrToBuf;

I am getting compilation error as:

: error C2440: '=' : cannot convert from 'unsigned char *' to 'unsigned char [4100]'

why is it sooo?? Please Help
agent_suwastica 13-Nov-13 2:21am    
Try to use:

*gchOBDResponseBuf = *myview->ptrToBuf;

if you are getting an error please post more codes.
Lekshmi KR 13-Nov-13 4:18am    
I tried the above code and the error have gone. but the buffer gchOBDResponseBuf is not having the content of buffer pointed by ptrToBuf. Why is it so?
agent_suwastica 13-Nov-13 4:37am    
How did you know that the buffer gchOBDResponseBuf is having an unexpected content?
If the pointer ptrToBuf is having the expected content then the buffer gchOBDResponseBuf must be correct.
Lekshmi KR 13-Nov-13 4:38am    
While debugging i found it out the content in the buffer is not same as that of ptrToBuf

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900