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
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343