-
June 21st, 2004, 12:36 PM
#1
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
-
June 21st, 2004, 12:49 PM
#2
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
-
June 22nd, 2004, 03:11 PM
#3
How about CStdioFile? I'm thinking not.
____________
VC++ .NET MFC
-
June 23rd, 2004, 02:14 AM
#4
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
}
}
-
June 23rd, 2004, 08:19 AM
#5
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
-
June 23rd, 2004, 09:22 AM
#6
OK, now I understand better.
What about this way:
void main ()
{
system ("type.exe 1.txt >> 2.txt");
}
-
June 23rd, 2004, 10:23 AM
#7
sbubis,
I appreciate your help. I would perfer not spawing a process to do this for me.
Any other ideas maybe?
____________
VC++ .NET MFC
-
June 23rd, 2004, 11:40 AM
#8
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
-
June 23rd, 2004, 12:21 PM
#9
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
-
July 1st, 2004, 02:29 AM
#10
#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
}
}
-
July 1st, 2004, 03:47 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|