Skip to content

Instantly share code, notes, and snippets.

@zjs1224522500
Last active May 14, 2020 05:29
Show Gist options
  • Save zjs1224522500/6229af6964dfe95ee62197f6f8249b64 to your computer and use it in GitHub Desktop.
Save zjs1224522500/6229af6964dfe95ee62197f6f8249b64 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdlib.h>
#include<unistd.h>
#include <sstream>
using namespace std;
typedef struct _FileBuffer
{
void* data;
long long data_len;
int id;
int* intPointer;
}FileBuffer;
typedef struct _TestStruct
{
void* data;
}TestStruct;
FileBuffer* getFileBuffer(const char* path);
TestStruct* getTestStruct(const char* path);
int main() {
const char* value = "testetst";
const char* path = "/c++/hik_obj/data/data-down.txt";
FILE* downloadFile = fopen(path, "wb+");
fwrite(value, sizeof(char), strlen(value), downloadFile);
fclose(downloadFile);
char * buffer;
long size;
ifstream file(path, ios::in|ios::binary|ios::ate);
size = file.tellg();
file.seekg(0, ios::beg);
buffer = new char[size];
file.read(buffer, size);
void* buffer_void = file.rdbuf();
// char* newBuffer = new char[size];
// memcpy(newBuffer, buffer_void, size);
// cout << "Buffer : " << (char *)buffer_void << endl;
cout << "Old Buffer:" << endl;
FileBuffer* oldBuffer;
cout << "Buffer is : " << oldBuffer << endl;
cout << "Buffer size address:" << &oldBuffer->data_len << endl;
cout << "Buffer size:" << oldBuffer->data_len << endl;
cout << "Buffer data address:" << &oldBuffer->data << endl;
cout << "Buffer data :" << oldBuffer->data << endl;
cout << "Buffer id address:" << &oldBuffer->id << endl;
cout << "Buffer id :" << oldBuffer->id << endl;
cout << "Buffer int pointer address:" << &oldBuffer->intPointer << endl;
cout << "Buffer int pointer:" << oldBuffer->intPointer << endl;
// cout << "Buffer int pointer value:" << *oldBuffer->intPointer << endl;
cout << "***************************************************************" << endl;
cout << "new Buffer:" << endl;
oldBuffer = new FileBuffer;
cout << "Buffer is : " << oldBuffer << endl;
cout << "Buffer size address:" << &oldBuffer->data_len << endl;
cout << "Buffer size:" << oldBuffer->data_len << endl;
cout << "Buffer data address:" << &oldBuffer->data << endl;
cout << "Buffer data :" << oldBuffer->data << endl;
cout << "Buffer id address:" << &oldBuffer->id << endl;
cout << "Buffer id :" << oldBuffer->id << endl;
cout << "Buffer int pointer address:" << &oldBuffer->intPointer << endl;
cout << "Buffer int pointer:" << oldBuffer->intPointer << endl;
// cout << "Buffer int pointer value:" << *oldBuffer->intPointer << endl;
cout << "***************************************************************" << endl;
cout << "allocate Buffer:" << endl;
oldBuffer->data = new char[100];
memcpy(oldBuffer->data, "testets", sizeof("testets"));
char* str_pointer = (char *) oldBuffer->data;
oldBuffer->intPointer = new int(10);
oldBuffer->id = 11;
oldBuffer->data_len = 9999;
cout << "Buffer is : " << oldBuffer << endl;
cout << "Buffer size address:" << &oldBuffer->data_len << endl;
cout << "Buffer size:" << oldBuffer->data_len << endl;
cout << "Buffer data address:" << &oldBuffer->data << endl;
cout << "Buffer data :" << oldBuffer->data << endl;
cout << "Buffer data value:" << str_pointer << endl;
cout << "Buffer id address:" << &oldBuffer->id << endl;
cout << "Buffer id :" << oldBuffer->id << endl;
cout << "Buffer int pointer address:" << &oldBuffer->intPointer << endl;
cout << "Buffer int pointer:" << oldBuffer->intPointer << endl;
cout << "Buffer int pointer value:" << *oldBuffer->intPointer << endl;
cout << "***************************************************************" << endl;
delete oldBuffer->data;
delete oldBuffer->intPointer;
delete oldBuffer;
cout << "Delete Buffer:" << endl;
cout << "Buffer is : " << oldBuffer << endl;
cout << "Buffer size address:" << &oldBuffer->data_len << endl;
cout << "Buffer size:" << oldBuffer->data_len << endl;
cout << "Buffer data address:" << &oldBuffer->data << endl;
cout << "Buffer data :" << oldBuffer->data << endl;
cout << "Buffer data value:" << str_pointer << endl;
cout << "Buffer id address:" << &oldBuffer->id << endl;
cout << "Buffer id :" << oldBuffer->id << endl;
cout << "Buffer int pointer address:" << &oldBuffer->intPointer << endl;
cout << "Buffer int pointer:" << oldBuffer->intPointer << endl;
cout << "Buffer int pointer value:" << *oldBuffer->intPointer << endl;
cout << "***************************************************************" << endl;
// cout << "File Buffer:" << endl;
// FileBuffer* fileBuffer = getFileBuffer("/c++/hik_obj/data/testdata-down.txt");
// cout << "Buffer is : " << fileBuffer << endl;
// cout << "Buffer size address:" << &fileBuffer->data_len << endl;
// cout << "Buffer size:" << fileBuffer->data_len << endl;
// cout << "Buffer data address:" << fileBuffer->data << endl;
// cout << "***************************************************************" << endl;
// cout << "Delete Buffer:" << endl;
// delete fileBuffer;
// cout << "Buffer is : " << fileBuffer << endl;
// cout << "Buffer size address:" << &fileBuffer->data_len << endl;
// cout << "Buffer size:" << fileBuffer->data_len << endl;
// cout << "Buffer data address:" << fileBuffer->data << endl;
// cout << "Buffer is null ? " << !fileBuffer << endl;
// TestStruct* testStruct = getTestStruct("/c++/hik_obj/data/data.txt");
// cout << "TestStruct is : " << testStruct << endl;
// cout << "TestStruct data:" << testStruct->data << endl;
// delete testStruct;
// cout << "TestStruct is : " << testStruct << endl;
// cout << "TestStruct data:" << testStruct->data << endl;
// cout << "TestStruct is null ? " << !testStruct << endl;
return 0;
}
FileBuffer* getFileBuffer(const char* path) {
FileBuffer* fileBuffer = new FileBuffer;
ifstream file(path, ios::in|ios::binary|ios::ate);
fileBuffer->data_len = file.tellg();
file.seekg(0, ios::beg);
fileBuffer->data = file.rdbuf();
file.close();
return fileBuffer;
}
TestStruct* getTestStruct(const char* path) {
TestStruct* testStruct = new TestStruct;
ifstream file(path, ios::in|ios::binary|ios::ate);
file.seekg(0, ios::beg);
testStruct->data = file.rdbuf();
file.close();
return testStruct;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment