FileToGrid (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses a button, a string grid, and an Open dialog box on a form. When you click the button, you are prompted for a file name. When you click OK,the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first time you use this application, you should do a "Fill Grid" and then a "Write Grid to File". Always use files generated like this to do a "Read File into Grid".

Read File to Grid

Code

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int iFileHandle;
  int iFileLength;
  int iBytesRead;
  int iLength;
  int count;
  wchar_t *pszBuffer;
  if (OpenDialog1->Execute())
  {
	try
	{
	  iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead);
      iFileLength = FileSeek(iFileHandle,0,2);
	  FileSeek(iFileHandle,0,0);
	  iBytesRead = FileRead(iFileHandle, &count, 4);
	  StringGrid1->ColCount = count;
	  iBytesRead = FileRead(iFileHandle, &count, 4);
	  StringGrid1->RowCount = count;
	  for (int x = 0; x < StringGrid1->ColCount; x++)
	  {
		for (int y = 0; y < StringGrid1->RowCount; y++)
		{
		  // Write out the length of each string, followed by the string itself.
		  FileRead(iFileHandle, &iLength, 4);
		  pszBuffer = new wchar_t[iLength + 1];

		  FileRead(iFileHandle, pszBuffer, iLength*sizeof(wchar_t));
		  pszBuffer[iLength] = 0;
		  StringGrid1->Cells[x][y] = pszBuffer;
		  delete [] pszBuffer;
		}
	  }
	  FileClose(iFileHandle);
	  delete [] pszBuffer;
	}
    catch(...)
	{
#if defined(_DELPHI_STRING_UNICODE)
	  Application->MessageBox(
		L"Can't perform one of the following file operations: Open, Seek, Read, Close.",
		L"File Error",
		IDOK);
#else
	  Application->MessageBox(
		"Can't perform one of the following file operations: Open, Seek, Read, Close.",
		"File Error",
		IDOK);
#endif
	}
  }
}

// Write Grid into File

#include <dir.h>

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  wchar_t szFileName[MAXFILE+4];
  int iFileHandle;
  int iLength;
  int count;
  if (SaveDialog1->Execute())
  {
	if (FileExists(SaveDialog1->FileName))
    {
	  _wfnsplit(SaveDialog1->FileName.c_str(), 0, 0, szFileName, 0);


	  wcscat(szFileName, L".BAK");
	  //Removing previously created .BAK files, if found
	  if (FileExists(szFileName)) {
		  DeleteFileW(szFileName);
	  }
	  RenameFile(SaveDialog1->FileName, szFileName);
    }
	iFileHandle = FileCreate(SaveDialog1->FileName);
	// Write out the number of rows and columns in the grid.
	count = StringGrid1->ColCount;
	FileWrite(iFileHandle, &count, sizeof(count));
	count = StringGrid1->RowCount;
	FileWrite(iFileHandle, &count, sizeof(count));
	for (int x=0;x<StringGrid1->ColCount;x++)
	{
	  for (int y=0;y<StringGrid1->RowCount;y++)
	  {
		// Write out the length of each string, followed by the string itself.
		iLength = StringGrid1->Cells[x][y].Length();
		FileWrite(iFileHandle, (wchar_t *)&iLength, sizeof(iLength));
		FileWrite(iFileHandle, StringGrid1->Cells[x][y].w_str(), StringGrid1->Cells[x][y].Length()*sizeof(wchar_t));
	  }
	}
	FileClose(iFileHandle);
  }
}

Uses