The Open Source Swiss Army Knife

/code/c/unix_c/unbufferedIO/
/code/c/unix_c/unbufferedIO/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /c   /unix_c   /unbufferedIO 
Permalink: readblock_test2.c
Title: add
article options : please login   |  raw source view  

#include <fcntl.h> // O_RDONLY
#include <unistd.h>
#include <sys/stat.h> // open, stat and statbuf (get_filesize function)
#include "../common/common.h"
/*  the read function's third argument is only important for the blocksize coming to&from disk */
/*  NOTHING TO DO with size to read; see r_read.c  */

ssize_t readblock(int fd, void *buf, size_t size);

int 
get_filesize(char *path)
{
struct stat statbuf;

if (stat(path, &statbuf) == -1)
return (-1);
else
return (int) statbuf.st_size;
}


int main(void) {
	int small_buffer_size=12;
	int filesize = 0;
	int counter = 0;
	size_t size_to_read = 0; // NOT necessarily int but POSIX says must be unsigned int
	char * filename = "readblock.c";
	char * buf;
	ssize_t bread = 0;
	filesize = get_filesize(filename);
	if (filesize == -1) {
		printf("problem getting filesize, returned -1 meaning ERROR\n\n");
		return -1;
	}
	if (small_buffer_size < (int)filesize)  {			/* take the smaller of the two values  */
		size_to_read = small_buffer_size;
	} else  {
		size_to_read = filesize;
	}
	if ((buf = (char *)malloc(size_to_read+1)) == NULL) {
		printf("malloc error for %d bytes\n\n", size_to_read);
	}
	memset(buf, 'h', size_to_read+1);
	buf[size_to_read]='\0';
	printf("original: %s\n", buf);
	printf("before reading %d: (values are one less because of array notation)\n", (int)size_to_read);
	printf("at position %d the value  %c (hex = %x)\n", size_to_read-2, buf[size_to_read-2], buf[size_to_read-2]);  
	printf("at position %d the value  %c (hex = %x)\n", size_to_read-1, buf[size_to_read-1], buf[size_to_read-1]);  // print last character read
	printf("at position %d the value  %c (hex = %x)\n", size_to_read, buf[size_to_read], buf[size_to_read]);  
	int fd = open(filename, O_RDONLY);
	if (fd < 0) {
		printf("error in %s for reading\n", filename);
		return -1;
		}
	/*  bread = readblock(fd, buf, filesize);  will GULP entire file */ 
	bread = readblock(fd, buf, size_to_read);  /* should not GULP entire file */
	if (bread == -1) {
		printf("read error\n\n");
		return -1;
	}
	printf("size_to_read %d == %d bread", size_to_read, bread);
	/* printf("the %d first bytes of %s in hexadecimal ", size_to_read, filename); */
	// while (buf) {  // keeps going and going ... SEGFAULTINg: question answered: does not append SHIT onto buf
//		printf(" %x ", *buf);
//		buf++;
//	}
	 while (counter < size_to_read) {  
		counter++;
	}
	// okay since counter starts at 0 and bread is always > 0
	/* printf("later: %s\n", buf);  */
	printf("after reading %d: (values are one less because of array notation)\n", (int)size_to_read);
	printf("at position %d the value  %c (hex = %x) \n", counter-1, buf[counter-1], buf[counter-1]);  
	printf("at position %d the value  %c (hex = %x)\n", counter, buf[counter], buf[counter]);  // print last character read
	printf("at position %d the value  %c (hex = %x)\n", counter+1, buf[counter+1], buf[counter+1]);  
	return 0;
}




Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top