BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
    HANDLE hFile;
    BOOL bSuccess = FALSE;

    hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwTextLength;

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

            pszText = GlobalAlloc(GPTR, dwBufferSize);
            if(pszText != NULL)
            {
                if(GetWindowText(hEdit, pszText, dwBufferSize))
                {
                    DWORD dwWritten;

                    if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                        bSuccess = TRUE;
                }
                GlobalFree(pszText);
            }
        }
        CloseHandle(hFile);
    }
    return bSuccess;
}

got this sample code from Forger's Win32 API Programming Tutorial

How could I modify this so that when I edit a file and save it, it won't have to go through about renaming it and saving it again as if it is a new file? Anyway, this is just for a school project.

Thanks a lot! XD

How could I modify this so that when I edit a file and save it, it won't have to go through about renaming it and saving it again as if it is a new file?

Most files are too variable to edit in place. Here are some guidelines for working with files:

  • Updating Records: Unless the file has a strict format where the starting address of each record can be calculated, the only way to change records is to rewrite the whole file. Otherwise the starting address can be calculated and then just the one record can be overwritten by seeking to that address.
  • Appending Records: This can be done without rewriting the whole file by seeking to the end or opening in append mode.
  • Inserting Records: This can not be done without rewriting everything in the file past the point of insertion, regardless of format. Just like an array, if you do not make room for the new record, another record will be erased.

    If the order of records does not matter, and the format is strict enough to calculate the starting address, you can save the record that would be erased, overwrite it, and then append it. This will usually be faster than rewriting everything past the point of insertion.

  • Deleting Records: If the format allows deleted or empty records and the starting address of each record can be calculated you can overwrite the record with a null record, otherwise this cannot be done without rewriting everything in the file past the point of deletion. Just like an array, deleting leaves a hole that needs to be filled in.
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.