Skip to content

Instantly share code, notes, and snippets.

@StrikeW
Created February 17, 2017 03:19
Show Gist options
  • Save StrikeW/bf4ad4598beb7525fd653e6700601e34 to your computer and use it in GitHub Desktop.
Save StrikeW/bf4ad4598beb7525fd653e6700601e34 to your computer and use it in GitHub Desktop.
C++ string当作buffer来使用
// 2017.2.17
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
struct yfs_dir_entry {
uint32_t inode; /* Inode number */
uint16_t rec_len; /* Directory entry length */
uint8_t name_len; /* Name length, word-aligned */
uint8_t file_type; /* T_DIR or T_FILE */
char name[255]; /* File name */
} __attribute__((packed));
int main(int argc, char *argv[])
{
struct yfs_dir_entry data = {13, 0, 1, 2, {'.', '\0', '\0', '\0' }};
struct yfs_dir_entry *de;
char *cbuf = (char*)(&data);
std::string buf(cbuf, sizeof(data));
printf("buf: sizeof(data)=%d, length=%d\n", sizeof(data), buf.length());
de = (struct yfs_dir_entry *)buf.c_str();
printf("inode=%d, rec_len=%d, name_len=%d, file_type=%d, name=%s\n",
de->inode, de->rec_len, de->name_len, de->file_type,
de->name);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment