Reading a binary file in text or ASCII format | Unix Linux Forums | Programming

  Go Back    


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

Reading a binary file in text or ASCII format

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-06-2005
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.


thanks
Nagendra
Sponsored Links
    #2  
Old 09-06-2005
RishiPahuja's Avatar
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
MySQL

Nagendra,

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.


Code:
strings binaryFileName

hope it helps.

rishi
Sponsored Links
    #3  
Old 09-07-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 November 2014, 10:33 PM EST
Location: NM
Posts: 10,282
Thanks: 288
Thanked 815 Times in 757 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
Code:
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:

Code:
#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)
                 continue;
             else
                 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) );
   *size=st.st_size;   
   *mode=st.st_mode;
   buffer=calloc(1,*size+1);
   ck(readall(fd, buffer, size) );    
   ck(close(fd) );
}


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

The Following User Says Thank You to jim mcnamara For This Useful Post:
figaro (09-10-2011)
    #4  
Old 12-03-2008
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
how to convert an ASCII file into binary format? (in the C shell) serendipity1276 Shell Programming and Scripting 1 07-16-2009 05:34 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 01:11 AM.