Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Checking type of File Data

Posted on 1997-12-23
7
196 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 99
windows 10 how make picture as desktop background 2 65
if loop in java 3 151
FizzBuzz challenge 9 82
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

792 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