Reading/Searching a file using a binary ifstream.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 10982 Views - Last Post: 14 January 2010 - 11:02 AM Rate Topic: -----

#1 Cyclone   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 13-February 09

Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 01:41 AM

I want to get a specific number of bytes from a file and insert them into an array. Then compare the new array with a predefined array to see if they are the same.

I want to do this with an ifstream. There are many different functions and I kinda know how to use them. I could use seekg and then use a loop but I don't know if that is the best way. Isn't there a way to get a set number of bytes at specific location? I used ifstream.read and specified 5 bytes but it always prints giberish to the console after it prints the 4 bytes.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Searching a file using a binary ifstream.

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 02:23 AM

I am too sleepy to write an example, but you have the basic idea... you use seekg() to position the file pointer to where you want to begin, than you read in a chunk of data, Now you can't print out the data like it is a string because it is not "text" data -- it is binary data (i.e. it may not have a zero to terminate the string)... so you have to print it out one byte at a time (or sanitize the data by making sure that all the data is text and can be displayed).


Even though it is not a terribly good example of what you want to do (sorry but I am too tiered to program effectively) you can look at this snippet which grabs a buffer of like 1024 chars (which it then displays in HEX)... Though that is probably not a great program to look at.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 02:32 AM

here is a little example...
#include <iostream>
#include <fstream>

const char PROGRAM_NAME[] = "read10Bytes.exe";
static const int Error_InvalidUsage = 1;
static const int Error_InvalidFile = 2;

int main(int argc, const char* argv[]) {
	if (argc != 2) {
		std::cout << "\nUsage: " << PROGRAM_NAME << " filename\n" << std::endl;
		std::exit(Error_InvalidUsage);
	}
	std::fstream binfile(argv[1], std::ios::in | std::ios::binary | std::ios::out);
	if (!binfile.is_open()) {
		std::cout << "\nCould not open file: " << argv[1] << std::endl;
		std::exit(Error_InvalidFile);
	}

	std::cout << "Moving to address 10 and reading 10 bytes...." << std::endl;
	binfile.seekg(10);
	char buffer[11];
	binfile.read(buffer, 10);
	buffer[10]=0; // adding terminating zero...
	std::cout << "And the buffer held\"" << buffer << "\"" << std::endl;
	binfile.close();
	return 0;
}


I ran the program and asked it to look at its own source code file... the result was:
Moving to address 10 and reading 10 bytes....
And the buffer held"iostream>
"

note that because there was a newline and the of the 10 bytes there is a newline in the output...
Was This Post Helpful? 0
  • +
  • -

#4 Cyclone   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 13-February 09

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 02:35 AM

View PostNickDMax, on 10 Jan, 2010 - 03:23 AM, said:

Now you can't print out the data like it is a string because it is not "text" data -- it is binary data (i.e. it may not have a zero to terminate the string)... so you have to print it out one byte at a time (or sanitize the data by making sure that all the data is text and can be displayed).


Thanks Nick. I've been working on this and am getting closer. Is that why the code below prints giberish at the end? Isn't there a way to add the "\0" null at the end of the array?

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char ch;
	char complete[4];

	ifstream in("c:/test.txt", ios::in | ios::binary);
	
	//in.seekg(10, ios::beg);

	for(int i=0; i<=2; i++)
	{
		in.get(ch);
		complete[i] = ch;
	}
	
	cout << complete;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 02:36 AM

Define it empty : char complete[4]={0};

Or you could tag it at the end of your loop, since the i variable is nice & fresh & still relevant :) :

	for(int i=0; i<=2; i++)
	{
		in.get(ch);
		complete[i] = ch;
	}
	complete[i+1]='\0';


Was This Post Helpful? 0
  • +
  • -

#6 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 02:37 AM

yes... there is nothing to tell cout to STOP printing... so it keeps going until it finds a \0 char...
Was This Post Helpful? 0
  • +
  • -

#7 Cyclone   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 13-February 09

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 03:20 AM

Ahh that makes sense now... binary streams unformatted... so no '\0' escape character.

Is using a loop slower then using the ifstream.read function?

Thanks for the help. The simple things seem to get me confused. ;)
Was This Post Helpful? 0
  • +
  • -

#8 Cyclone   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 13-February 09

Re: Reading/Searching a file using a binary ifstream.

Posted 10 January 2010 - 11:01 PM

OK I got the program working great and can seach and compare a file for a word using the above methods.

but... I can't seem to convert the input stream into an integer array for comparing pure binary data that is not in a text file. All I can do is input the stream into a character array. Do I have to use a built in function such as atoi() or will that cause conversion errors?

Thanks.

This post has been edited by Cyclone: 10 January 2010 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#9 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Reading/Searching a file using a binary ifstream.

Posted 11 January 2010 - 02:06 AM

For converting strings to integers, you can use a stringstream to convert it painlessly :) Here's an example:
#include <iostream>
#include <sstream>
using namespace std;

int main() {
	char array[] = { '2', '3', ' ', '6', '\0' }; //a dummy array containing some numbers
	stringstream sstr;
	sstr<<array; //insert the array into the stringstream
	int i, j; //we want to extract the numbers to these variables
	sstr>>i>>j; //extract the first int element to i, second to j
	cout<<i<<endl; //prints 23, which is the first integer element
	cout<<j<<endl; //prints the second int element which is 6
	return 0;
}

the default extraction operator separates elements by space character. The Above example shows basically how to convert from string to int.
Hope that helped you.

Edit: this way of converting requires you to have only integers in the array. So you have to make sure they are int and space separated.

This post has been edited by Anarion: 11 January 2010 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 11 January 2010 - 06:51 AM

Well... my first question would be... if you need to read text formatted data why would you open a file for binary? I mean there no need to use stringstreams if you are getting input from a text file -- the file input stream can convert "23" into 23 just as easily as a stringstream.

reading a file in binary is great if you need to say read the bytes 0x08 0x09 in as 2312...
Was This Post Helpful? 1
  • +
  • -

#11 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Reading/Searching a file using a binary ifstream.

Posted 11 January 2010 - 09:56 AM

View PostNickDMax, on 11 Jan, 2010 - 04:21 PM, said:

Well... my first question would be... if you need to read text formatted data why would you open a file for binary? I mean there no need to use stringstreams if you are getting input from a text file -- the file input stream can convert "23" into 23 just as easily as a stringstream.

reading a file in binary is great if you need to say read the bytes 0x08 0x09 in as 2312...

Yes Yes, this ability comes from the streams (same as filestream). I assumed converting from an array alone (not when reading them from a file stream)... my bad :D

This post has been edited by Anarion: 11 January 2010 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#12 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Reading/Searching a file using a binary ifstream.

Posted 11 January 2010 - 10:00 AM

you could initialize your buffer to NULL before you populate it

char buffer[1024];
memset(buffer,'\0',1024);


This post has been edited by ImaSexy: 11 January 2010 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#13 Cyclone   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 13-February 09

Re: Reading/Searching a file using a binary ifstream.

Posted 14 January 2010 - 08:51 AM

Thanks for the input. I found that using a cast works for writing\reading integers.

out.write((char*)&n,byte_count);



I got the program to search\read\write bytes\text.

The last thing i'm trying to figure out is how to calculate the number of bytes a decimal number takes to write to the file.

For example:
decimal value of 255 takes up 1 byte(FF).
decimal value of 65535 takes up 2 bytes(FF FF).

and so on.

I have spent hours trying to figure out the math. It should be easy. I tried dividing by 255 and then counting how many times it takes to reach 255 etc. Even multiplying, dividing by base 16. If I use a calculator and divide a hex number by 255 I can get the correct results. But in c++ I don't know how to convert the number into hex so I can divide it by FF. If that makes sense.
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 14 January 2010 - 09:11 AM

to tell how many bytes a particular number uses you would look at the log base 256... (or the log base 2 and divide by 8).

But really,

short int is 16bits (2 bytes)
int, long int are 32 bits (4 bytes)
long long is 64bits (8 bytes)

a 16 bit short pointer (not really used any more) is 2 bytes
a 32 bit far pointer is 4 bytes
a 64 bit far pointer (used on 64bit processors) is 8 bytes...

These are all pretty standard.
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Reading/Searching a file using a binary ifstream.

Posted 14 January 2010 - 09:16 AM

OR -- a better way, use the sizeof() operator to determine the size of the short/int/long etc. on your platform.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2