Private Function IsTextFile(FileName As String) As Boolean
    Dim FF As Integer
    Dim FileData() As Byte
    Dim K As Long
    
    FF = FreeFile
    Open FileName For Binary Access Read As FF
        ReDim FileData(LOF(FF) - 1)
        Get FF, , FileData
    Close FF
    
    For K = 0 To UBound(FileData)
        If FileData(K) > 126 Then
            IsTextFile = False
            Exit For
        End If
    Next K
    
    If K = UBound(FileData) + 1 Then IsTextFile = True
End Function

Please help me
Thanks

I have no idea what language the original is, but here's a guess as to what it might mean in C++ pseudocode:

Declare a function called IsTextFile that takes a single argument of type string called FileName and returns type bool.  In the body of the function do the following:
1) declare variables:
bool result = true;
int K = 0;
vector<int> FileData
ifstream FF(FileName.c_str());

2) read data from file into FileData
while(FF >> K)
   FileData.push_back(K);
FF.close();

3) evaluate data
for(K = 0; K < FileData.length; ++K)
   if(FileData[K] > 126)
      result = false;

4) return result

I think it opens the file, gets the file length, allocates a buffer if the required size, then checks if any character elements is > 126, which is the upper limit of the text characters in the standard ascii chart.

So I would think this would work. Note: the following is untested and not compiled.

bool IsTextFile(string FileName)
{
    bool isText = true;
   // open the file in binary mode
    ifstream in( FileName , ios::binary);
  if( in.is_open() )
  {
      size_t len;
      // locate end of file
      in.seekg(0, ios::end);
      // get file length
     len = in.tellg();
     // back to beginning of the file
     in.seekg(0,ios::begin);
    // allocate buffer space
     unsigned char* buf = new [size+1];
    // read the file into memory
    in.read(static_cast<char*>(buf), size);
    in.close();

    // now check if its a text file
    for(size_t i = 0; i < size && isTest == true; i++)
   {
        if( buf[i] < 13 ||  buf[i] > 126)
        {
            isText = false;
        }
    }
    delete[] buf;
    return isText;
}

There is another way to accomplish the same thing without allocating any memory. Just read the file one character at a time and test to see if it is a text character or not.

This article has been dead for over six months. Start a new discussion instead.