Hi,

This is probably some basic knowledge but I haven't found it out.

I have this snippet of code that is causing me problems:

HANDLE hFile; 
		char lpBuffer[100] ="";
		char idBuffer[100] = "";
		
		
		DWORD ofs;
		strcpy(lpBuffer, fingerprint);
		strcat(lpBuffer,"\0");
		


		hFile = CreateFile(buffer,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
		WriteFile(hFile,lpBuffer,sizeof(lpBuffer), &ofs, NULL);
		strcpy(buffer,"");
		if(ReadFile(hFile, idBuffer, 100, &ofs, NULL))
			printf("OK");
		else
			printf("NOTO OK");// Suppose there is more data to be read and GetLastError is IO_PENDING
	
		
		
  printf("idBuffer = %s\n", idBuffer);
	printf("¨fingerprint = %s\n", fingerprint);

It is just for test purposes since I can't get it working. Everything works fine except that ReadFile won't fill the idBuffer with any data from the file. The result code from ReadFile says OK so no problem there. If I open the file I can see the data but it is not picked up to variable idBuffer.

CreateFile and Write File seems to be working fine because I get the expected data in the file. And then again ReadFile does not read it....

I'm using Visual Studio 2005 if that is of any interest.

Anyone having the solution to this out there?


Regards,
Kalle

Recommended Answers

All 3 Replies

After creating and writing the file, what are you expecting to read? Where's the file pointer after a write?

After creating and writing the file, what are you expecting to read? Where's the file pointer after a write?

I'm expecting to read from the beginning of the file. Thank you for your questions! They made me open my eyes. I closed the filehandle and opened the file again and now it works. ReadFile read the file earlier but in the wrong place.

Thank you for your answer!

Instead of closing and reopening the file you can just move the file pointer back to the beginning of the file. Call SetFilePointer()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.