data file help

Reply

Join Date: May 2003
Posts: 1
Reputation: gilgamesh is an unknown quantity at this point 
Solved Threads: 0
gilgamesh gilgamesh is offline Offline
Newbie Poster

data file help

 
0
  #1
May 14th, 2003
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?
Reply With Quote Quick reply to this message  
Join Date: May 2003
Posts: 2
Reputation: evenstar is an unknown quantity at this point 
Solved Threads: 0
evenstar evenstar is offline Offline
Newbie Poster

Re: data file help

 
0
  #2
May 14th, 2003
Originally Posted by gilgamesh
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

  1. HANDLE hFile = INVALID_HANDLE_VALUE;
  2. hFile = CreateFile("yourfile.dat",
  3. GENERIC_READ | GENERIC_WRITE,
  4. 0, // no file sharing
  5. NULL, // no security descriptor
  6. OPEN_ALWAYS, // creates if not found
  7. FILE_ATTRIBUTE_NORMAL,
  8. 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.

  1. if (hFile != INVALID_HANDLE_VALUE)
  2. {
  3. DWORD bytesWritten;
  4. if (FALSE == WriteFile(hFile,
  5. dataBuffer, // data to write
  6. dataBufferSize,
  7. &bytesWritten,
  8. NULL)) // not using overlapped I/O
  9. {
  10. MessageBox(GetActiveWindow(),
  11. "Error", "Error", MB_ICONSTOP);
  12. }
  13. // will want to verify
  14. // bytesWritten == dataBufferSize
  15. }

Be sure to close the file with
  1. CloseHandle(hFile);

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC