User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 361,909 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,483 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2811 | Replies: 1
Reply
Join Date: May 2003
Posts: 1
Reputation: gilgamesh is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gilgamesh gilgamesh is offline Offline
Newbie Poster

data file help

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2003
Posts: 2
Reputation: evenstar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
evenstar evenstar is offline Offline
Newbie Poster

Re: data file help

  #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

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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC