Click to See Complete Forum and Search --> : Creating a Text Stream


ZComplx
July 10th, 2002, 11:25 AM
I've used in "fstream.h" to create text files in c++, but can I do the same functionality in embedded? I want to write, and open text files.

alanr
July 10th, 2002, 01:44 PM
You can use CFile....

undiwahn
July 10th, 2002, 03:24 PM
A lot of the standard libraries just arn't available on an embedded platform.

If you're targeting WinCE in some form, you'll have to use the Win32 API (CreateFile(), ReadFile() ) or the MFC wrapper for that ( CFile, as noted above ).

ZComplx
July 10th, 2002, 03:25 PM
Can I just get a simple example of how to create a file and then write and read to it? Thanks for your help

undiwahn
July 10th, 2002, 03:37 PM
I suggest referencing it on msdn online (msdn.microsoft.com).

But to start you off:

void SimpleOpenAndRead(void)
{
HANDLE hFile = CreateFile( "Filename.ext", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

if(hFile == INVALID_HANDLE_VALUE)
return; // Didn't open successfully

char Buffer[51]; // We'll read up to 50 bytes from the file
DWORD BytesRead; // This'll hold how many bytes we actually read
if(!ReadFile( hFile, (void*)&Buffer, 50, &BytesRead, NULL ))
return; // couldn't read from the file

Buffer[BytesRead] = 0; // Hack -- terminate string with a 0;

AfxMessageBox(Buffer); // Display what we read

CloseHandle(hFile);
}