CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: File appending

  1. #1
    Join Date
    Aug 2003
    Posts
    84

    File appending

    I have an existing file that I would like to append information to after the tool has been used. The current file size can be anyware from 1K to 1MB containing raw text data. Any simple way to do this?
    ____________
    VC++ .NET MFC

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    1) Open the file for append
    2) Make sure the file pointer is moved to the end of the file.

    The exact syntax fould depend on the way you are handling the file (FILE, CFile, STL implementation, other)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2003
    Posts
    84
    How about CStdioFile? I'm thinking not.
    ____________
    VC++ .NET MFC

  4. #4
    Join Date
    Jun 2004
    Posts
    102
    Here I put two variants of append-to-file.
    Choose one of them, comment other and run...

    //* Using Win32 API
    #include <windows.h>

    void main ()
    {
    HANDLE hFile;
    char *pNewRecord;
    DWORD dwTmp;

    //* Open an existing file
    hFile = CreateFile ("C:\\TEMP\\TEST.TXT",
    GENERIC_READ | GENERIC_WRITE, 0, 0,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if (hFile != INVALID_HANDLE_VALUE)
    {
    //* Set file pointer to the end of the file
    SetFilePointer (hFile, 0, 0, FILE_END);

    //* Write something
    pNewRecord = "This record is appended to the file\r\n";
    WriteFile (hFile, pNewRecord, strlen (pNewRecord), & dwTmp, 0);
    //* Close file
    CloseHandle (hFile);
    }
    else
    {
    //* Error message
    }
    }

    //***************************************************
    //* Using C library
    #include <stdio.h>

    void main ()
    {
    FILE *pFile;

    //* Open an existing file
    pFile = fopen ("C:\\TEMP\\TEST.TXT", "a+");
    if (pFile)
    {
    //* Write something
    fprintf (pFile, "This record is appended to the file\n");
    //* Close file
    fclose (pFile);
    }
    else
    {
    //* Error message
    }
    }

  5. #5
    Join Date
    Aug 2003
    Posts
    84
    Thanks, I appreciate the help. I am familiar with the approach you presented me, however, it doesn't fit my needs. Apologise if this is a communication issue on my part.

    What I am trying to accomplish is what the 'ol DOS command 'TYPE' is used for. Taking the entire contents one 1 file and conjoining it into another.

    i.e type 1.txt >> 2.txt
    ____________
    VC++ .NET MFC

  6. #6
    Join Date
    Jun 2004
    Posts
    102
    OK, now I understand better.
    What about this way:

    void main ()
    {
    system ("type.exe 1.txt >> 2.txt");
    }

  7. #7
    Join Date
    Aug 2003
    Posts
    84
    sbubis,
    I appreciate your help. I would perfer not spawing a process to do this for me.

    Any other ideas maybe?
    ____________
    VC++ .NET MFC

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Use the above provided example, then loop through the file tht you want to append......
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Aug 2003
    Posts
    84
    Originally posted by TheCPUWizard
    Use the above provided example, then loop through the file tht you want to append......

    Which provided sample?
    ____________
    VC++ .NET MFC

  10. #10
    Join Date
    Jun 2004
    Posts
    102
    #include <windows.h>

    void main ()
    {
    HANDLE hFileTo, hFileFrom;

    //* Open an existing file
    hFileTo = CreateFile ("C:\\TEMP\\TEST.TXT",
    GENERIC_READ | GENERIC_WRITE, 0, 0,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    hFileFrom = CreateFile ("C:\\TEMP\\APPEND.TXT",
    GENERIC_READ, 0, 0,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if ((hFileTo != INVALID_HANDLE_VALUE) &&
    (hFileFrom != INVALID_HANDLE_VALUE))
    {
    //* Set file pointer to the end of the file
    SetFilePointer (hFileTo, 0, 0, FILE_END);

    DWORD dwSize, dwTmp;
    char *pBuffer;

    //* Since your file is less than 1Mb:
    dwSize = GetFileSize (hFileFrom, NULL);
    pBuffer = new char [dwSize];

    ReadFile (hFileFrom, pBuffer, dwSize, & dwTmp, NULL);

    WriteFile (hFileTo, pBuffer, dwSize, & dwTmp, 0);

    delete [] pBuffer;

    //* Close file
    CloseHandle (hFileTo);
    CloseHandle (hFileFrom);
    }
    else
    {
    //* Error message
    }
    }

  11. #11
    Join Date
    Aug 2003
    Posts
    84
    sbubis,
    Thanks for that snippet. You've been a great help.
    ____________
    VC++ .NET MFC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured