LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2012, 06:55 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Rep: Reputation: Disabled
buffering


Hi all,

i need to compute hash code for a file , currently im reading entire file at once and i perform hashing ,but i need to read a file chunk by chunk basis (say 512bytes) using a buffer and hash it and update it, im finding little difficult to read chunk by chunk basis ,please someone help me

Code:
long ComputeHashCode(string fname)
{
  int length,buflen;
  char * buffer;
  unsigned long adler = 1L;
 // checksum_calculator chk;
  unsigned int cs = 0;
  ifstream is;
 // chk.init();

  is.open (fname, ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  
  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);
  is.close();

 // cs=chk.calculate(buffer);
  adler = update_adler32(adler,(unsigned char*) buffer, length);

//  cout.write (buffer,length);

  delete[] buffer;
  return adler;
}
 
Old 04-11-2012, 07:57 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Why is this marked Solved? Did you solve it? If so can you post your solution?
 
Old 04-11-2012, 08:53 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
This would be a lot easier in C. Here's a starting point that's comparable to what you have:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#define BUFFER_SIZE 512


unsigned long ComputeHashCode(const char *filename)
{
	//allocate a buffer (you can use any method)
	static char buffer[BUFFER_SIZE];

	unsigned long adler = 1L;

	//open the file without buffering
	int current_file = open(filename, O_RDONLY);
	if (current_file < 0)
	{
	fprintf(stderr, "could not open '%s': %s\n", filename, strerror(errno));
	return 0;
	}

	//keep reading 'BUFFER_SIZE' bytes until there is nothing left to read
	int read_size = 0;
	while ((read_size = read(current_file, buffer, BUFFER_SIZE)) > 0)
	{
// 	write(STDOUT_FILENO, buffer, read_size);
	adler = update_adler32(adler, (unsigned char*) buffer, read_size);
	}

	//close the current file
	close(current_file);

	return adler;
}


int main(int argc, char *argv[])
{
	//(this just loops through the filenames provided as arguments)
	for (int I = 1; I < argc; I++)
	{
	unsigned long adler = ComputeHashCode(argv[I]);
	fprintf(stdout, "%lu\n", adler);
	}

	return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Double Buffering Ace Blackwell Programming 3 02-13-2009 09:19 AM
Kaffeine always Buffering AvatarofVirgo Linux - General 1 10-13-2005 04:46 AM
pipeline buffering jk3us Linux - Software 2 12-17-2004 07:52 PM
turn off buffering suchi_s Programming 2 10-30-2004 07:14 AM
Regarding buffering with streams karthikvina Programming 0 09-04-2003 02:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration