i am currently using the writefile() windows api function in my program, i want to store the follow in 1 buffer but i'm unsure on how to do this exactly

"===================================" '\n'
	"Date: " localTime->tm_mon "/" localTime->tm_mday  "/" localTime->tm_year '\n'
	"Time: " localTime->tm_hour ":" localTime->tm_min  "." localTime->tm_sec '\n'
	 "===================================" '\n'
	 "[ERROR] - " ErrorString '\n'

i want to store it all in 1 buffer so i only have to do 1 call to writefile...i could have a buffer for each of the above segments for example

BYTE *buffer1 = "===================================;"
      BYTE *buffer2 = '\n';
      etc...

but i dont want to have to call consecutive called to writefile when i can only make one. Any advice is greatly appreciated! thx

Recommended Answers

All 6 Replies

I'm unfamiliar with Windows programming, I've never seen the BYTE type, but presumably you can create a char* object containing everything and convert to a BYTE*, in which case you can use the standard cstring functions.

char buffer[100];
buffer[0] = 0; // not needed, but I tend to do it.
strcpy (buffer, "abcd");
strcat (buffer, "efgh");
strcat (buffer, "ijkl");

buffer now contains "abcdefghijkl". Use strlen to get the length. Typecast it and/or convert it to BYTE* and use your Windows function.

Or you can use stringstreams sort of like this

#include <string>
#include <sstream>
#include <iostream>
#include <windows>

int main ()  {
   std::stringstream format;
   format << "asd" << 5 << "gdfg\n";
   
   const char* to_write=format.str().c_str();   

   unsigned long readbytes;

   HANDLE hf=CreateFile ("asd.txt",GENERIC_READ|GENERIC_WRITE,
                          0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);


   WriteFile (hf,to_write,format.str().size(),&readbytes,0);
 
 }

I'm replying to your private message here if you don't mind.I find it easier.If you want to code strictly in C you should go for the C string library as VernonDozier suggested.

template <class T>
void add_log(const T ErrorString)
{
	HANDLE hFileHandle;
	if( ( hFileHandle = CreateFile(L"log.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ) ) == INVALID_HANDLE_VALUE ) 
	 return; // throw GetLastError();

    SYSTEMTIME st;
    GetLocalTime(&st);

	std::wstringstream buffer;

	buffer << L"===================================\n" 
		   << L"Date: " << st.wMonth << L'/' << st.wDay    << L'/' << st.wYear   << L'\n'
		   << L"Time: " << st.wHour  << L':' << st.wMinute << L'.' << st.wSecond << L'\n'
		   << L"===================================\n" 
		   << L"[ERROR] - " << ErrorString << L'\n';

	const wchar_t* cStringBuffer = buffer.str().c_str();
 if(WriteFile( hFileHandle, &cStringBuffer, wcslen(cStringBuffer) , NULL, NULL ) == NULL )
  return;//throw GetLastError();

 
 CloseHandle( hFileHandle );
}

ok i did as you stated but for some reason the writefile api() is causing an access violation...and im not sure why? any ideas

Try replacing the NULL argument before the last with a valid address as i did.That's the argument that will receive the number of bytes written.

thanks! it worked

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.