Home

Files Operations

     

Copying a File

You can make a copy of a file from one directory to another. To do this, you can call the CopyFile() function of the Win32 library. Its syntax is:

BOOL WINAPI CopyFile(
  __in  LPCTSTR lpExistingFileName,
  __in  LPCTSTR lpNewFileName,
  __in  BOOL bFailIfExists
);

Not only does this function copy a file, it can actually be used to perform many types of operations:

  • You may want to check first whether the file exists. If it does, then you may want to copy it. To do this, pass the same path and the same name of the file for the first and second arguments. Pass the third argument as FALSE
  • To make a copy of the same file in the same directory, that is, to get two files with the same contents but of course different names, provide the same path but different names in the first and second arguments. Pass the third argument as TRUE. Here is an example:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCopyFileClick(TObject *Sender)
    {
    	CopyFile("C:\\Exercise\\sample.txt",
    		 "C:\\Exercise\\example.txt",
    		 TRUE);
    }
    //---------------------------------------------------------------------------
  • To make a copy of the same file from one directory to another directory but the file using the same name, provide different paths in the first and second arguments but keep the same name for the file. Pass the third argument as TRUE. Here is an example:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCopyFileClick(TObject *Sender)
    {
    	CopyFile("C:\\Exercise1\\sample.txt",
    		 "C:\\Exercise2\\sample.txt",
    		 TRUE);
    }
    //---------------------------------------------------------------------------
  • To make a copy of the file from one directory to another directory but create a new name of the file in the other directory, provide different paths in the first and second arguments and a different name for the file in the second argument. Pass the third argument as TRUE. Here is an example:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnCopyFileClick(TObject *Sender)
    {
    	CopyFile("C:\\Exercise1\\sample.txt",
    		 "C:\\Exercise2\\example.txt",
    		 TRUE);
    }
    //---------------------------------------------------------------------------

Moving a File

If you copy a file from one directory to another, you would have two copies of the same file or the same contents in two files. Instead of copying, if you want, you can simply move a file from one directory to another. This operation can be performed by calling the MoveFile() function. Its syntax is:

BOOL WINAPI MoveFile(
  __in  LPCTSTR lpExistingFileName,
  __in  LPCTSTR lpNewFileName
);

The arguments of this function are the same as those of the CopyFile() function. The CopyFile() function supports at least two operations:

  • To move a file to a different directory but keep the same name for the file, provide a different path in both arguments but use the same name for the file in both arguments. Here is an example:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnMoveFileClick(TObject *Sender)
    {
    	MoveFile("C:\\Exercise1\\sample.txt",
    		 "C:\\Exercise2\\sample.txt");
    }
    //---------------------------------------------------------------------------
  • To create a new file in a different directory based on a kown file, provide different paths in the first and second arguments and provide a different name for the file in the second argument. Here is an example:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnMoveFileClick(TObject *Sender)
    {
    	MoveFile("C:\\Exercise1\\sample.txt",
    		 "C:\\Exercise2\\example.txt");
    }
    //---------------------------------------------------------------------------

Checking the Existence of a File

Before creating a file, you may first want to find out whether that file exists already so you would not overwrite it. In the same way, in order to open a file, its must exist. In some or most cases, if you try opening a file that cannot be found, you would recive an error. To assist you getting this information, the VCL provides the FileExists function. Its C++ equivalent from the Object Pascal syntax is:

bool FileExists(const String FileName);

Here are exampes:

Sphere Calculation

//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "Evaluation.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmEvaluation *frmEvaluation;
//---------------------------------------------------------------------------
__fastcall TfrmEvaluation::TfrmEvaluation(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmEvaluation::btnCalculateClick(TObject *Sender)
{
    double dRadius = 0.00;

    try {
	dRadius = StrToFloat(edtRadius->Text);
	edtDiameter->Text = FloatToStrF(dRadius * 2, ffFixed, 12, 5);
	edtCircumference->Text = FloatToStrF(dRadius * 2 * M_PI,
					     ffFixed, 12, 5);
	edtArea->Text = FloatToStrF(dRadius * dRadius * 4.0 * M_PI,
				    ffFixed, 12, 5);
	edtVolume->Text = FloatToStrF(dRadius * dRadius *
				      dRadius * 4.0 * M_PI / 3.00,
				      ffFixed, 12, 5);
    }
    catch (EConvertError *)
    {
	ShowMessage(L"The value you provided for the radius is not acceptable");
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------
void __fastcall TfrmEvaluation::btnSaveClick(TObject *Sender)
{
    int iFileHandle;
    double dRadius;
    TFileName FileName = edtFileName->Text;

    if( FileExists(FileName) )
    {
	// It looks like a file with that name exists already.
	// Ask the user if the file should be replaced
	if( Application->MessageBox(
			L"A file with that name exists already.\n"
			L"Do you want to replace it?",
			L"Sphere Calculation",
			MB_YESNO | MB_ICONQUESTION) == IDYES )
	{
		// If the user decides to replace the file, do it
		// Get the value of the radius
		dRadius = StrToFloat(edtRadius->Text);

		// Create the file
		iFileHandle = FileCreate(edtFileName->Text);

		// Write the value of the radius to the file
		FileWrite(iFileHandle, &dRadius, sizeof(double));

		// Reset the form
		edtRadius->Text        = L"";
		edtDiameter->Text      = L"";
		edtCircumference->Text = L"";
		edtArea->Text          = L"";
		edtVolume->Text        = L"";

		// Close the file
		FileClose(iFileHandle);
		}
		else
		{
			// Since the user clicked No, display a message.
		ShowMessage(L"You should then provide another name for the file.");
			// Don't do anything else
			return;
		}
	}
	else // It appears that this is a new file
	{
         // Get the value of the radius
		dRadius = StrToFloat(edtRadius->Text);

		// Create the file
		iFileHandle = FileCreate(edtFileName->Text);

		// Write the value of the radius to the file
		FileWrite(iFileHandle, &dRadius, sizeof(double));

		// Reset the form
		edtRadius->Text        = L"";
		edtDiameter->Text      = L"";
		edtCircumference->Text = L"";
		edtArea->Text          = L"";
		edtVolume->Text        = L"";

		// Close the file
		FileClose(iFileHandle);
        }
}
//---------------------------------------------------------------------------
void __fastcall TfrmEvaluation::btnOpenClick(TObject *Sender)
{
    int iFileHandle;
    double dRadius = 0.00;
    TFileName FileName = edtFileName->Text;

    if( FileExists(FileName) )
    {
	iFileHandle = FileOpen(FileName, fmOpenRead);

	FileRead(iFileHandle, &dRadius, sizeof(double));

	edtRadius->Text = FloatToStrF(dRadius, ffFixed, 12, 5);
	btnCalculateClick(Sender);

	FileClose(iFileHandle);
    }
    else
	ShowMessage(L"There is no such a file.");
}
//---------------------------------------------------------------------------
 
 
 

Deleting a File

If you have an existing file you don't need anymore, you can delete it. To perform this operation in the VCL, you can call the DeleteFileA() function. Its syntax is:

bool __fastcall DeleteFileA(System::UnicodeString FileName);

Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnDeleteFileClick(TObject *Sender)
{
    if( FileExists(L"C:\\Exercise\\soho.txt") )
    {
	if( Application->MessageBox(
			L"Do you really want to delete that file?",
			L"File processing",
			MB_YESNO | MB_ICONQUESTION) == IDYES )
        {
	    DeleteFileA(L"C:\\Exercise\\soho.txt");
	}
    }
}
//---------------------------------------------------------------------------

To perform this same operation using the Win32 library, you can call the DeleteFile() function Its syntax is:

BOOL WINAPI DeleteFile(__in  LPCTSTR lpFileName);

When calling this function, pass the name of, or the path (relative or complete) to, the file. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnDeleteFileClick(TObject *Sender)
{
    if( FileExists(L"C:\\Exercise\\sample.txt") )
    {
	if( Application->MessageBox(
			L"Do you really want to delete that file?",
			L"File processing",
			MB_YESNO | MB_ICONQUESTION) == IDYES )
        {
	    DeleteFile(L"C:\\Exercise\\sample.txt");
        }
    }
    else
	ShowMessage(L"There is no such a file.");
}
//---------------------------------------------------------------------------

Renaming a File

If you have an existing file whose name you don't want anymore, you can rename it. To perform this operation, you can call the Object Pascal's RenameFile() function. Its C++ suntax appears as:

bool RenameFile(const String OldName, const String NewName);

Pass the first argument as the name of the file you want to rename. Pass the second argument as the name name. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnRenameFileClick(TObject *Sender)
{
	RenameFile("C:\\Exercise1\\example.txt",
		 "C:\\Exercise1\\sample.txt");
}
//---------------------------------------------------------------------------

In reality, you can use this function as you would the Win32's MoveFile() function. For example, you can call the RenameFile() function to move a file from one directory keep the same name of creating a new name. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnRenameFileClick(TObject *Sender)
{
	RenameFile("C:\\Exercise1\\example.txt",
		   "C:\\Exercise2\\sample.txt");
}
//---------------------------------------------------------------------------
 
 
   
 

Home Copyright © 2010-2011 FunctionX, Inc.