0

i need to be able to add data to my .dat file without deleting my old data during a program run can anyone help me?

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by evenstar
0

i need to be able to add data to my .dat file without deleting my old data during a program run can anyone help me?

If this is a console app (DOS program) or UNIX program that outputs to standard output, you could redirect the output to your data file

program.exe >> datafile.dat


If this is a Windows program, you can use CreateFile with the following arguments

HANDLE hFile = INVALID_HANDLE_VALUE;
hFile = CreateFile("yourfile.dat", 
  GENERIC_READ | GENERIC_WRITE, 
  0, // no file sharing
  NULL, // no security descriptor
  OPEN_ALWAYS, // creates if not found
  FILE_ATTRIBUTE_NORMAL,
  NULL); // No template handle

This returns a HANDLE that you can pass to ReadFile and WriteFile

Using SetFilePointer if you need to position to any part of the file.

if (hFile != INVALID_HANDLE_VALUE)
{
  DWORD bytesWritten;
  if (FALSE == WriteFile(hFile,
    dataBuffer, // data to write
    dataBufferSize,
    &bytesWritten,
    NULL))  // not using overlapped I/O
  {
    MessageBox(GetActiveWindow(),
      "Error", "Error", MB_ICONSTOP);
  }
  // will want to verify 
  // bytesWritten == dataBufferSize
}

Be sure to close the file with

CloseHandle(hFile);

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.