Solved

Checking type of File Data

Posted on 1997-12-23
7
195 Views
Last Modified: 2013-11-19
I am trying to find a way to check if the data in a file is binary or text.  I tried to use CFile Open with CFile::typeText as a flag, but I get an exception and informed that this flag is not supported.  Can someone please tell me how I can go into a file pick a portion of the data and verify that the data is text data or binary data.  I need to be able to verify the portion picked is not text data. When I go into MSVC and open the file it tells me weather or not this data is text, so there must be a way.  Please supply the code needed to do this.
Thanks
0
Comment
Question by:sjm
7 Comments
 
LVL 2

Expert Comment

by:Srw
ID: 1313019
All files are just byte streams.  There really is no Text vs. Binary.  Those file opening modes just refer to how whitespace and CR/LF pairs are handled.

A file's type (text / binary) is a more or less an arbitrary distinction.  Your requirements determine if the file is "binary"  So, why must you have a "binary" file?  What is it about a "text" file that is bad for you?
0
 
LVL 23

Accepted Solution

by:
chensu earned 200 total points
ID: 1313020
Take a look at Previously Asked Windows Programming Question: "Determine if a file is ascii or binary" (http://www.experts-exchange.com/topics/bin/Q.8630024172). I am sorry that it will take you 5 more Points. Because there are some comments, you'd better see the original question.

BTW, CFile::typeText is used in derived classes only (for example, CStdioFile).
0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313021
Hi sjm:  My thoughts on this topic are as follows:

Each Byte of text data always has values less than 128.
Text only uses the low seven bits of an eight bit byte;
while binary data uses all eight bits and binary bytes
can have values as high as 255. Thus you can determine
a file contains binary and not text if it has byte values
in excess of 127 in it. You cannot determine a file is
text and not binary (because text is basically a subset of binary) and just because a file contains no byte values
greater than 127 does not mean it is not a binary file,
although it could certainly be considered a text file.

The code to check would be as follows:

CString strFileName = "c:\TestFile";

//CREATE A CFile OBJECT TO OPEN THE FILE
CFile* pFile;
TRY
{
      pFile = new CFile(
            strFileName,
            CFile::modeRead | CFile::shareDenyWrite );
}
CATCH( CFileException, e )
{
      if( e->m_cause != CFileException::none )
      MessageBox( "Cannot open this file." , "FILE ACCESS ERROR" , MB_OK );
      return;
}
END_CATCH

//ALLOCATE MEMORY TO READ THE FILE
DWORD dwReadSize = pFile->GetLength();
HANDLE hFile = GlobalAlloc(GMEM_FIXED, dwReadSize0);
if( !hFile )
{
      MessageBox("Unable to allocate RAM to read the file.", "MEMORY ALLOCATION ERROR", MB_OK);
      delete pFile;
      return;
}
BYTE* pbyFile = (BYTE*)hFile;

//READ THE FILE
UINT nBytesRead = pFile->Read(pbyFiler, dwReadSize);
if( nBytesRead != dwReadSize )
{
      MessageBox("Cannot read this file.", "FILE READ ERROR", MB_OK);
      delete pFile;
      GlobalFree(hFIle);
      return;
}

//TEST FOR A BINARY FILE
BOOL bFileIsText = TRUE;
for(DWORD; i<dwReadSize; i++)
{
      if(pbyFile[i] > 127)
      {
            bFileIsText = FALSE;
            break;
      }
}

//AT THIS POINT bFileIsText WILL TELL TYPE OF FILE

//CLEAN UP
delete pFile;
GlobalFree(hFie);

Hope that helps out.

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 23

Expert Comment

by:chensu
ID: 1313022
The text files WBerthin is referring to are pure ASCII English text files. It does not apply to the text files in other languages.
0
 

Author Comment

by:sjm
ID: 1313023
Thanks for all the input but the points go to WBerthin.  He gave me the code to find the answer.  WBerthin please send another comment in so I can give you the points.

0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313024
I am pleased if my code helped ...
BUT I think the points belong to chensu!
I just sent a comment on his locked question,
to help out if I could.
Maybe sometime chensu will help out one of my answers.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1313025
sjm:
If you would like to give the points to WBerthin, you should reopen the question and ask WBerthin to answer it. Otherwise, it is still locked.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 147
conditional code and condition difference 9 82
fizzArray  challenge 1 82
has77  challenge 9 83
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question