I can't figure out how to make my save function append to the txt file it always overwrites. Also, I'm trying to figure out how to make each set of words on a newline in the text file. Here is the function:

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_ALWAYS, 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, FILE_END))
                        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;
}

Recommended Answers

All 7 Replies

from what i can gather from my msdn you will need to read the file file before you write to it so that the file pointer is at the end and will put any new text after everything in the file. you can use the ReadFile() function to read all the data and move the file pointer to the end. hopefully this will help.

Thank you!

Hi,

For my self i would first try to create the file with CREATE_NEW parameters, as specified, CreateFile will return an error if the file exist, and create and open it if not.

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

In case that CreateFile Return an error, you only have to call CreateFile with the OPEN_EXISTING to finish to gain access to your file.

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

On this is done you have access to your file, but don't forget to place the pointer of the file to the end of it to dont overwrite your existing content with this call :

BOOL SetEndOfFile(HANDLE hFile);

Hoping this help, best regards.

do you now how to make each section saved go to a newline in the text file?

Hi,

For my self i would first try to create the file with CREATE_NEW parameters, as specified, CreateFile will return an error if the file exist, and create and open it if not.

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

In case that CreateFile Return an error, you only have to call CreateFile with the OPEN_EXISTING to finish to gain access to your file.

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

On this is done you have access to your file, but don't forget to place the pointer of the file to the end of it to dont overwrite your existing content with this call :

BOOL SetEndOfFile(HANDLE hFile);

Hoping this help, best regards.

I did what you said bdusablon, but I got this error.
error C2373: 'SetEndOfFile' : redefinition; different type modifiers
Here is how I implemented it:

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);
			BOOL SetEndOfFile(HANDLE 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
            }
	    
		}
	}

the prototype for SetEndOfFile is BOOL SetEndOfFile(HANDLE hFile); so you do not need to have BOOL SetEndOfFile(HANDLE hFile); all you need to do is write SetEndOfFile(hFile); and if you want you could assign the return value to a bool variable to check for success or failure ie.

bool isSet;
isSet = SetEndOfFile(hFile);
if (!isSet)
{
// do something..
}

Thats'it! I think Everything has been said on the subject.
I would also recommend to find a good tutorial on file access to learn basic of file access in C.

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.