Hi,

Is it possible to read a file only to get the first 100 bytes from the begining and the last 100 bytes at the end? I have to get info from files that are in the first 100 bytes and in the last 100 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files take "forever" to get the first and last 100 bytes. Thank you!

Ofcourse it is possible, but if you want us to implement and send you the code that's not gonna happen.

Maybe you should send what you've got, then we may give you some ideas.

#include <stdio.h>

int main (int argc, const char * argv[]) {
    
		FILE *fp;
		// Read only the first 10 bytes for test instead of 100
		int values[10];
		int i;
		fp = fopen("/Users/tenix/Desktop/test/P1000701.JPG", "rb");
		fread(values, 1, 10, fp);
		for(i = 0; i < 10; i++)
		{
			printf("%X \n", values[i]);
		}
		fclose(fp);
		return 0;
}

I test with an JPG and got this output, but it is not correct when i open a hex editor and check the bytes:

[Session started at 2010-05-29 01:56:34 +0200.]
E1FFD8FF
78459E6F
6669
8FE005BC
0
0
0
0
0
0

The Debugger has exited with status 0.

redeclare values as unsigned char instead of int. Then your program will most likely display the data the same as your hex editor did.

You did not post the code you tried to get the last 100 bytes.

Thank you for all your help! I am getting to understand the C concept.
Here is what i did with fgetc() to get character instead of fread() to get block.
It works well for me. The code looks ugly, please correct me if i am wrong.
Thank you again!

To get the first 10 bytes:

#include <stdio.h>

int main (int argc, const char * argv[]) {
	unsigned char buffer[10];
	FILE *source;
	int i, c;
	
	source = fopen("/Users/tenix/Desktop/test/P1010023.JPG", "r+b");
	fseek(source, 0, 10); // Set the new position at 10
	
	if (source != NULL) {
		for (i = 0; i < 10; i++) {
			c = fgetc(source); // Get character
			buffer[i] = c; // Store characters in array
			printf("Result A: %X\n", c);
		}
	} else {
	       printf("File not found.\n");
	       return 0;
	}

	fclose(source);
	
	/* Add null to end string */
	buffer[i] = '\0';
	
	for (i = 0; i < sizeof(buffer); i++) {
		printf("Result B: %X\n", buffer[i]);
	}
	
	return 0;
}

To get the last 10 bytes:

#include <stdio.h>

int main (int argc, const char * argv[]) {
	unsigned char buffer[10];
	FILE *source;
	int i, c;
	long size;
	
	source = fopen("/Users/tenix/Desktop/test/P1010023.JPG", "r+b");
	fseek(source, 0, SEEK_END); // Set the new position to the end of file
	size = ftell(source); // Get the current position
	fseek(source, size-10, SEEK_SET); // Set the new position at the beginning
	
	if (source != NULL) {
		for (i = 0; i < 10; i++) {
			c = fgetc(source); // Get character
			buffer[i] = c; // Store characters in array
			printf("Result A: %X\n", c);
		}
	} else {
	       printf("File not found.\n");
	       return 0;
	}

	fclose(source);
	
	/* Add null to end string */
	buffer[i] = '\0';
	
	for (i = 0; i < sizeof(buffer); i++) {
		printf("Result B: %X\n", buffer[i]);
	}
	
	return 0;
}

You can combine lines 10, 11 and 12 to use just one fseek() fseek(source, -10, SEEK_END); That will set the pointer 10 bytes from the end of the file.

ahh! -10 is new to me. Clever solution. Thank you very much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.