Hey, I'm trying to make a save function in API that will append to a txt file instead of writing over it. Can I get some help? Here's what I have so far:

#include <new>
#include "windows.h"

BOOL SaveTextFileFromEdit(HWND hEdit, HWND hEdit2, HWND hEdit3, HWND hEdit4, HWND hEdit5, HWND hEdit6, HWND hEdit7, HWND hEdit8, HWND hEdit9, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
		CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

//hEdit
		if(hFile != INVALID_HANDLE_VALUE)
		 {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
	    
		}
		else
		{
		hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		SetEndOfFile(hFile);
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit,pszText, dwBufferSize))
                {
                    DWORD dwWritten;
                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
	    
		}
	}


//hEdit2			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit2);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit2,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit3			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit3);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit3,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit4
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit4);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit4,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit5		
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit5);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit5,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit6			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit6);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit6,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit7			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit7);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit7,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit8			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit8);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit8,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	}
//hEdit9			
	if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

        dwTextLength = GetWindowTextLength(hEdit9);
        // No need to bother if there's no text.
        if(dwTextLength > 0)
        {
            char* pszText;
            DWORD dwBufferSize = dwTextLength + 1;

            try
            {
                pszText = new char[dwBufferSize];
                if(GetWindowText(hEdit9,pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                delete[] pszText;
            }
            catch(...)
            {
                // allocation failed
            }
		}
	
	
	}
	CloseHandle(hFile);
}
return bSuccess;
}

Have you tried opening with FILE_APPEND_DATA instead of GENERIC_WRITE?

is that all I have to change?

apparently not I just tried it and it's still overwriting it.

This works. But frankly I don't like using those win32 api file i/o functions because standard c++ std::fstream is a lot easier to work with.

#include <windows.h>
using namespace std;

int main()
{
    HANDLE hFile = CreateFile("TextFile1.txt", GENERIC_READ|GENERIC_WRITE,
            0,0,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

    if( hFile != INVALID_HANDLE_VALUE)
    {
        char text[] = "This line was added\r\n";
        DWORD dwBytesWritten = 0;
        SetFilePointer(hFile, 0, 0, FILE_END);
        WriteFile(hFile,text, strlen(text), &dwBytesWritten, 0);
        CloseHandle(hFile);

    }
}
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.