C++Builder Handle Based File Operations In C++ Builder

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Handle Based File Operations In C++ Builder
February 27, 2021 By Yilmaz Yoru

Handle based file read and write methods can be used in Modern C++, In C++ Builder. In Modern C++ we highly recommend you to use Для просмотра ссылки Войди или Зарегистрируйся to support your application on all platforms and for the some benefits of using these methods like supporting worldwide languages by using UnicdeStrings etc. If you dont need these, you can easily use old methods in C++ Builder with GUIs. You can browse a file by using OpenDialog() component and read lines of a file by using fgets() or fscanf() or we can get parameters by using sscanf() function.

If you are new to C++ Builder please check this Для просмотра ссылки Войди или Зарегистрируйся video to create a new and modern C++ Builder projects.
C++:
FILE *fp;
 
OpenDialog1->Execute();
 
if(fp=fopen(AnsiString(OpenDialog1->FileName).c_str(), "rb"))
{
  do
  {
      fgets(s,255,fp);
      // fscanf(fp,"%d %d", &x, &y);
      // sscanf(s,"%d %d", &x, &y);
  } while (!feof(fp));
  fclose(fp);
}
To write on a file we can can browse folder and a file name by using SaveDialog() component, we can use fputs() or fprintf() functions as below
C++:
FILE *fp;
 
SaveDialog1->Execute();
 
if(fp=fopen(AnsiString(SaveDialog1->FileName).c_str(), "rw"))
{
  do
  {
      fputs(s,255,fp);
      // fprintf(fp,"%d %d", x, y);
  } while (!feof(fp));
  fclose(fp);
}
Another old method to read files is using FileHandle and FileOpen() method. We can read lines by using ReadLn() method.
C++:
int iFileHandle;
int iFileLength;
int iBytesRead;
PAnsiChar pszBuffer = 0;
String str;
try
{
   if(iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead | fmOpenWrite | fmShareDenyNone))
   {
      while(!Eof(iFileHandle))
      {
 ReadLn(iFileHandle, &str);
      }
      Memo1->Lines->Add(str);
    }
    else
    {
       Application->MessageBox(
 L"Can't perform one of the following file operations: Open, Seek, Read, Close.",
 L"File Error", IDOK);
 }
  }
  catch(...)
  {
 Application->MessageBox(
   L"Can't perform one of the following file operations: Open, Seek, Read, Close.",
   L"File Error", IDOK);
 delete [] pszBuffer;
  }
We can easily method to read files is using FileHandle and FileOpen() method. We can write lines by using WriteLn() method.
C++:
int iFileHandle;
int iFileLength;
int iBytesRead;
PAnsiChar pszBuffer = 0;
String str=L"Testing";
try
{
 if(iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead | fmOpenWrite | fmShareDenyNone))
 {
   while(!Eof(iFileHandle))
   {
 WriteLn(iFileHandle, str);
   }
 }
 else
 {
   Application->MessageBox(
 L"Can't perform one of the following file operations: Open, Seek, Read, Close.",
 L"File Error", IDOK);
 }
}
catch(...)
{
 Application->MessageBox(
   L"Can't perform one of the following file operations: Open, Seek, Read, Close.",
   L"File Error", IDOK);
 delete [] pszBuffer;
}