Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileCreate Problem 1

Status
Not open for further replies.

stylonurus

Programmer
Jun 7, 2006
19
US
I am creating a TComboBox which will contain a historical list of the last 10 IP addresses entered by the user. At startup, I check to see if the file where I save these addresses exists. I create it if it doesn't. If it does I load the previously saved list.
Code:
char const = *ComboFNpath  "C:\\NuVasive\\RemoteReader\\IPLIST.DAT";

  // check for existence of file
  if ( !FileExists(ComboFNpath) ) {
      // create file
      FileCreate(ComboFNpath);
  }
  else {
      Form1->ComboBox1->Items->LoadFromFile(ComboFNpath);
      // Insert first item in list into viewable area
      Form1->ComboBox1->Text = Form1->ComboBox1->Items->operator [](0) ;
  }
The previous segment works and creates the file. The problem is when I try to update the list with a saveToFile call after the user enters an IP address.
Code:
  if ( ( idx = Form1->ComboBox1->Items->IndexOf(Form1->ComboBox1->Text) ) >= 0 )   {
       // If present, move that item to top of list and perform connect
      Form1->ComboBox1->Items->Insert(0,Form1->ComboBox1->Text);
      Form1->ComboBox1->Items->Delete(idx+1) ;
      Form1->ComboBox1->Text = Form1->ComboBox1->Items->operator [](0) ;
      Form1->ComboBox1->Items->SaveToFile(ComboFNpath);

      strcpy(DestinationAddress,Form1->ComboBox1->Text.c_str());
      DestinationType = FALSE;
  }
I get this exception on the SaveToFile call above:

raised exception in class EFCreateError with message ' Cannot create file C:\NuVasive\RemoteReader\IPLIST.DAT' Process stopped.


But the file was created at startup and

This problem only happens on the first time when no list exists. The next time I run it I get no error and the Save and Load functions work perfectly. Does anyone have a clue why the first initial run causes problems. And of course since I'm kind of new to this programming environment any programming tips are more than welcome.
Thanks so much
Rick Eis
 
FileCreate creates the file specified by ComboFNpath and returns the handle of the created file. Your file then remains open until you FileClose() it, or your application terminates.

The SaveToFile() function tries to create the file anew, but since your application still has the file open it can't re-create it, hence the error.

There is no need to create the file using FileCreate() since the SaveToFile call will create it for you, write the data and close the file.

The code in your first snippet simply needs to be:
Code:
char const = *ComboFNpath  "C:\\NuVasive\\RemoteReader\\IPLIST.DAT";

  if (FileExists(ComboFNpath)) {
      Form1->ComboBox1->Items->LoadFromFile(ComboFNpath);
      // Insert first item in list into viewable area
      Form1->ComboBox1->Text = Form1->ComboBox1->Items->operator [](0) ;
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top