C program to read a binary file and search for a string?


 
Thread Tools Search this Thread
Top Forums Programming C program to read a binary file and search for a string?
# 1  
Old 05-17-2014
C program to read a binary file and search for a string?

Hi,

I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language bookSmilie

At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value after it.

While this is quite quick on a 50M file, it takes a while for a 500M file.

Can someone please advise where I can find some examples of reading a binary file that can search for a particular string and then exits when it finds it or maybe one that reads the first few bytes of a binary file where I assume the contents of the information is.

When running strings | grep, I assume it is running the strings on the whole 500M file and then doing a grep. I am hoping to be able to use a C program that will read only the first x bytes or read the binary file and exits once it found the information that it is looking for.

Any guidance much appreciated. Thanks in advance.
# 2  
Old 05-17-2014
Hi.

Using the GNU utilities, one could avoid resorting to c:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate searching binary file.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C strings head grep

FILE=${1-$(which cut)}

pl " Input data file $FILE is type $(file $FILE)"

pl " Results, strings -- $(wc -l < $FILE) strings found." 

pl " Results, strings and first 2 occurrences:"
strings $FILE |
head -2 

pl " Results, strings and grep searching for \"bucket\""
strings $FILE |
grep bucket

pl " Results, grep, (expect only a message):"
grep bucket $FILE

pl " Results, grep, every occurrence:"
grep -a bucket $FILE

pl " Results, grep, only first 2 results:"
grep -a -m 2 bucket $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
strings GNU strings (GNU Binutils for Debian) 2.18.0.20080103
head (GNU coreutils) 6.10
grep GNU grep 2.5.3

-----
 Input data file /usr/bin/cut is type /usr/bin/cut: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, stripped

-----
 Results, strings -- 130 strings found.

-----
 Results, strings and first 2 occurrences:
/lib64/ld-linux-x86-64.so.2
__gmon_start__

-----
 Results, strings and grep searching for "bucket"
# buckets:         %lu
max bucket length: %lu
# buckets used:    %lu (%.2f%%)

-----
 Results, grep, (expect only a message):
Binary file /usr/bin/cut matches

-----
 Results, grep, every occurrence:
# buckets:         %lu
max bucket length: %lu
# buckets used:    %lu (%.2f%%)

-----
 Results, grep, only first 2 results:
# buckets:         %lu
max bucket length: %lu

This uses command cut binary file as the file to be searched.

See man pages for details.

Best wishes ... cheers, drl
# 3  
Old 05-17-2014
Better to use something like this:
Code:
head -c 32m | grep $STRING

That addresses the problem of strings searching the entirety of large files when only the first few MB are to be searched. (The example will search the first 32 MB.)
# 4  
Old 05-18-2014
Or
Code:
dd if=file count=x | grep "string"

Note that x will default to 512 byte blocks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

How to search specific object in binary file?

I have a very important question: I have to find a table 'XXTO_AR_TABLE' in a folder present in server and in this folder these types files are present: .rdf, jar file, java class file etc. These are binary files. I want to get name of these files where my table named 'XXTO_AR_TABLE' is... (2 Replies)
Discussion started by: Vikash163
2 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Solaris

A string in a binary file

hi experts, anyone knows how can i find a string in a multiple binary files in a multiple directories and display the file name containing the string? i mean i have lots of folders with binary files and i am looking for specific string within one of these files i would like to know the right... (6 Replies)
Discussion started by: q8devilish
6 Replies

6. Shell Programming and Scripting

Python Binary File Read and Parse

Hi to everyone :), i have a challenge right now in python that for now needs a bit of help in one part of the c0de. The task is create a new file with the name of the file defined by the ASCII content between the 3 byte and the 16 byte that is parsed from the binary file, the file is over 20 Mb i... (0 Replies)
Discussion started by: drd0spt
0 Replies

7. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

8. Homework & Coursework Questions

C Program to search and read all named pipes in current directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a C program to search the current directory for all pipes. 1. It will print the pipe... (2 Replies)
Discussion started by: natwickley
2 Replies

9. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

10. Shell Programming and Scripting

replace string in binary file

Dear all, How can replace special string with another string in binary file? Thanks&Best Regards (2 Replies)
Discussion started by: sg1207
2 Replies
Login or Register to Ask a Question