Solved

Checking type of File Data

Posted on 1997-12-23
7
192 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now