Hi,
Could someone explain how to write some text to a file, my documentation is truly appalling.
PS some code to write text with my Dot-matrix printer would be cool too. :)
I am using the ANSI 32-bit RHIDE C++ compiler (DOS).
Thanks
Vlatko
Oct 14th, 2000, 01:39 PM
hEdit = handle of the window containing text
pszFileName = filename
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, NULL, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)// No need to bother if there's no text.
{
LPSTR pszText;
pszText = (char*)GlobalAlloc(GPTR, dwTextLength + 1);pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, NULL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
if(dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (char*)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0; // Null terminator
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE; // It worked!
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
parksie
Oct 14th, 2000, 05:03 PM
How's about:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream MyFile("file.txt");
char *pcText = "Here is text";
MyFile << pcText << endl;
MyFile.close();
}