943,479 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4584
  • C RSS
May 14th, 2003
0

data file help

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gilgamesh is offline Offline
1 posts
since May 2003
May 14th, 2003
0

Re: data file help

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evenstar is offline Offline
2 posts
since May 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Question
Next Thread in C Forum Timeline: Joystick driver





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC