Unix/Linux Go Back    

Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Reading a binary file in text or ASCII format


Thread Tools Search this Thread Display Modes
Old Unix and Linux 09-06-2005   -   Original Discussion by Nagendra
Nagendra Nagendra is offline
Registered User
Join Date: Sep 2005
Last Activity: 6 September 2005, 2:37 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Reading a binary file in text or ASCII format

Hi All,
Please suggest me how to read a binary file in text or ASCII format.

Sponsored Links
Old Unix and Linux 09-06-2005   -   Original Discussion by Nagendra
RishiPahuja's Unix or Linux Image
RishiPahuja RishiPahuja is offline
Registered User
Join Date: Apr 2005
Last Activity: 15 April 2011, 1:11 AM EDT
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts


There is no way to convert binary file to ascii format.These two are basic intrinsic file properties. meaning during creation itself you determine if the file has to be binary or ascii text. Binary file will be usually of some specified format say pdf/executable/word document etc. for converting to text you will have specific tools for specific formats to ascii/text etc.

But if you binary file contains some valid ASCII strings in it, say like error messages you can filter them using strings command.

strings binaryFileName

hope it helps.

rishi Linux
Sponsored Links
Old Unix and Linux 09-07-2005   -   Original Discussion by Nagendra
jim mcnamara jim mcnamara is offline Forum Staff  
Join Date: Feb 2004
Last Activity: 17 November 2017, 6:09 PM EST
Location: NM
Posts: 11,238
Thanks: 570
Thanked 1,116 Times in 1,030 Posts
This comes up all too often. Windows has text files. Unix does not. Unix is not Windows. In Unix a file is a file is a file. It's a bag of bytes. Period.

Because a standard C string uses ASCII zero (nul character) as the end of string, that data from files that contain nuls (in Windows these are binary files, in Unix they are just files) cannot be parsed as strings because the nuls confuse everything.

To the OP: try
od -c filename

to find out what is in the file. Then you will know if you can read it using standard string C calls like fgets(). Or if you will have to use fread().

After you've programmed for a while you tend to bypass fread and fgets, especially when you're dealing with large files that may contain interesting stuff.
This reads an entire file containing anything into a buffer:

#include <stddef.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>

#define ck(x) \
if( (x) == (-1) ){ perror("");exit(EXIT_FAILURE);}

/* read a buffer from a file */
ssize_t readall(int fd, void *buf, size_t *bytes){
     ssize_t nread = 0, n=0;
     size_t nbyte = *bytes;

     do {
         if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) {
             if (errno == EINTR)
                 return (-1);
         if (n == 0)
             return nread;
         nread += n;
     } while (nread < nbyte);
     return nread;

/* read control */
void readfile(char *fname, char *buffer, size_t *size, mode_t *mode)
   int fd=0;   
   struct stat st;
   ck(fd=open(fname,O_RDONLY) );
   ck(fstat(fd,&st) );
   ck(readall(fd, buffer, size) );    
   ck(close(fd) );

int main(int argc, char *argv[])
   char *buffer=NULL;
   size_t size;
   mode_t mode;
   /* play with buffer here  */
   return 0;  

The Following User Says Thank You to jim mcnamara For This Useful Post:
figaro (09-10-2011)
Old Unix and Linux 12-03-2008   -   Original Discussion by Nagendra
BlueCrystalEyes BlueCrystalEyes is offline
Registered User
Join Date: Dec 2008
Last Activity: 3 December 2008, 5:11 PM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Read Binary .dat file from linux, convert it to txt (windows readable) format

hi i am trying to read a binary file [.dat]. its in linux, i want that file to work in windows. but in windows i cannot read this file. could someone help me to read the dat file.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Binary to ASCII(TEXT converion) pistachio Shell Programming and Scripting 1 12-29-2009 03:00 PM
Binary or ascii file u263066 Shell Programming and Scripting 6 08-08-2008 12:19 PM
To convert multi format file to a readable ascii format gaur.deepti UNIX for Dummies Questions & Answers 5 03-25-2008 04:03 PM
ftp - determine ascii or binary file congo Shell Programming and Scripting 5 07-21-2007 04:53 PM

All times are GMT -4. The time now is 12:00 PM.