|
Работа через WinAPI
|
| int iFileHandle;
|
| int iFileLength;
|
| int iBytesRead;
|
| char *pszBuffer;
|
| if (OpenDialog1->Execute())
|
| {
|
| try
|
| {
|
| iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead);
|
| iFileLength = FileSeek(iFileHandle,0,2);
|
| FileSeek(iFileHandle,0,0);
|
| pszBuffer = new char[iFileLength+1];
|
| iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);
|
| FileClose(iFileHandle);
|
| for (int i=0;i<iBytesRead;i++)
|
| {
|
| StringGrid1->RowCount += 1;
|
| StringGrid1->Cells[1][i+1] = pszBuffer[i];
|
| StringGrid1->Cells[2][i+1] = IntToStr((int)pszBuffer[i]);
|
| }
|
| delete [] pszBuffer;
|
| }
|
| catch(...)
|
| {
|
| Application->MessageBox("Can't perform one of the following file operations: Open, Seek, Read, Close.", "File Error", IDOK);
|
| }
|
| }
|
| char szFileName[MAXFILE+4];
|
| int iFileHandle;
|
| int iLength;
|
| if (SaveDialog1->Execute())
|
| {
|
| if (FileExists(SaveDialog1->FileName))
|
| {
|
| fnsplit(SaveDialog1->FileName.c_str(), 0, 0, szFileName, 0);
|
| strcat(szFileName, ".BAK");
|
| RenameFile(SaveDialog1->FileName, szFileName);
|
| }
|
| iFileHandle = FileCreate(SaveDialog1->FileName);
|
|
|
| // Write out the number of rows and columns in the grid.
|
| FileWrite(iFileHandle, (char*)&(StringGrid1->ColCount), sizeof
|
| (StringGrid1->ColCount));
|
| FileWrite(iFileHandle, (char*)&(StringGrid1->RowCount), sizeof
|
| (StringGrid1->RowCount));
|
| 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, (char*)&iLength, sizeof
|
| (iLength));
|
| FileWrite(iFileHandle, StringGrid1->Cells[x][y].c_str(), StringGrid1->Cells[x][y].Length());
|
| }
|
| }
|
| FileClose(iFileHandle);
|
| }
|