简体   繁体   中英

How to read x characters from position y in a char * buffer?

I am reading through a buffer (char *) and i have a cursor, where i am tracking my starting position of the buffer, is there a way to copy characters 7-64 out of the buffer, or is my best bet to just loop the buffer from poistion x to position y?

The size of the destination buffer is the result of another function dynamically computed

Initializing this returns

variable-sized object 'version' may not be initialized

Relevant code parts:

int32_t size = this->getObjectSizeForMarker(cursor, length, buffer);
cursor = cursor + 8; //advance cursor past marker and size
char version[size] = this->getObjectForSizeAndCursor(size, cursor, buffer);

-

char* FileReader::getObjectForSizeAndCursor(int32_t size, int cursor, char *buffer) {
  char destination[size];
  memcpy(destination, buffer + cursor, size);
}

-

int32_t FileReader::getObjectSizeForMarker(int cursor, int eof, char * buffer) {
  //skip the marker and read next 4 byes
  cursor = cursor + 4; //skip marker and read 4
  unsigned char *ptr = (unsigned char *)buffer + cursor;
  int32_t objSize = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
  return objSize;

}

Move the pointer to buffer six units ahead (to get to the seventh index), and then memcpy 64-7 (57) bytes, eg:

const char *buffer = "foo bar baz...";
char destination[SOME_MAX_LENGTH];
memcpy(destination, buffer + 6, 64-7);

You may want to terminate the destination array so that you can work with it using standard C string functions. Note that we're adding the null character at the 58th index, after the 57 bytes that were copied over:

/* terminate the destination string at the 58th byte, if desired */
destination[64-7] = '\0'; 

If you need to work with a dynamically sized destination , use a pointer instead of an array:

const char *buffer = "foo bar baz...";
char *destination = NULL;

/* note we do not multiply by sizeof(char), which is unnecessary */
/* we should cast the result, if we're in C++ */
destination = (char *) malloc(58); 

/* error checking */
if (!destination) { 
    fprintf(stderr, "ERROR: Could not allocate space for destination\n");
    return EXIT_FAILURE;
}

/* copy bytes and terminate */
memcpy(destination, buffer + 6, 57);
*(destination + 57) = '\0';
...

/* don't forget to free malloc'ed variables at the end of your program, to prevent memory leaks */
free(destination); 

Honestly, if you're in C++, you should really probably be using the C++ strings library and std::string class. Then you can call the substr substring method on your string instance to get the 57-character substring of interest. It would involve fewer headaches and less re-inventing the wheel.

But the above code should be useful for both C and C++ applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM