memory - Reading part of a file in C using fread() and fseek() -


I am trying to read a file in buffer size block in BLOCK_SIZE (current My code initially reads the whole file (usually 2-4) to find the number of blocks to read it (usually 2-4), then to the same 1000 unsigned letters , then Repeats through the loop to read the file (" + 17 + filename ", which is required for everyone in the program later

However, for the first time only, when j = 1 , is this actually the data to the buff array. In other cases, when J! = 1 , Returns 0 .

I think the problem is either fseek ( ) Using to read the file before or before the memory allocation problem.

Any help can be found at 1000-1999 th The file should be appreciated for reading in the buf array.

Attached Distributing the relevant part:

  unsigned char * buf; Source = fopen (locat, "r"); Temp = filesize / BLOCK_SIZE + 1; For (j = 1; j & lt; = temp; j ++) {if (j == 1) {buf = (unsigned char *) molk ((size (unsigned char)) * (BLOCK_SIZE + 17+ filename )); Fred (buff + 17+ filename, size (unsigned char), BLOCK_SIZE, source); } Else if (j == temp) {buf = (unsigned char *) malloc ((sizeof (unsigned char)) * (filesize + 5 - BLOCK_SIZE * (j-1))); Disturb (source, BLOCK_SIZE * (j-1), SEEK_SET); // A warning Fred (buff + 5, size (unsigned char), file - BLOCK_SIZE * (j-1), closed by source); } Other {buf = (unsigned char *) malloc ((size (unsigned char)) * (5 + BLOCK_SIZE * (j-1))); Disturb (source, BLOCK_SIZE * (j-1), SEEK_SET); // Closed by a warning Fred (buff + 5, size (unsigned char), BLOCK_SIZE, source); } // do buf stuff with buf = "" here; Free (buf); }  

I would recommend checking the results and, in particular, sure Do that 0 - If it is not, then this can be a complete problem.

provided that fseek is being successful, Fred should read the total number of bytes you tell.

Besides, strlen is not necessarily a valid thing to use, because it assumes that it is a blank ending string. If the first letter you read is 0 bytes, strlen 0 will return. You are not treating it as a nonsensical end string (you are not allocating enough space for the oil faucet terminator - just what is necessary to fit your binary data), then maybe strlen is inappropriate.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -