mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 01:14:13 +00:00
Proposing use of C++ header files and functions (#4869)
* Proposing use of C++ header files and functions Proposing use of C++ header files and functions instead of C header file and functions. Here are few examples for comparison : C C++ <cstdio> <iostream> & <fstream> printf() cout fopen() ifstream etc ... Please let me know if there are any comments. * Updated diff based on review comments
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
55289c55bf
commit
c721009491
@@ -59,15 +59,18 @@ a `char *` array, which you pass to `GetMonster()`.
|
|||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||||
#include "flatbuffers/flatbuffers.h"
|
#include "flatbuffers/flatbuffers.h"
|
||||||
#include "monster_test_generate.h"
|
#include "monster_test_generate.h"
|
||||||
#include <cstdio> // For printing and file access.
|
#include <iostream> // C++ header file for printing
|
||||||
|
#include <fstream> // C++ header file for file access
|
||||||
|
|
||||||
FILE* file = fopen("monsterdata_test.mon", "rb");
|
|
||||||
fseek(file, 0L, SEEK_END);
|
std::ifstream infile;
|
||||||
int length = ftell(file);
|
infile.open("monsterdata_test.mon", std::ios::binary | std::ios::in);
|
||||||
fseek(file, 0L, SEEK_SET);
|
infile.seekg(0,std::ios::end);
|
||||||
|
int length = infile.tellg();
|
||||||
|
infile.seekg(0,std::ios::beg);
|
||||||
char *data = new char[length];
|
char *data = new char[length];
|
||||||
fread(data, sizeof(char), length, file);
|
infile.read(data, length);
|
||||||
fclose(file);
|
infile.close();
|
||||||
|
|
||||||
auto monster = GetMonster(data);
|
auto monster = GetMonster(data);
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@@ -78,9 +81,9 @@ If you look in your generated header, you'll see it has
|
|||||||
convenient accessors for all fields, e.g. `hp()`, `mana()`, etc:
|
convenient accessors for all fields, e.g. `hp()`, `mana()`, etc:
|
||||||
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||||
printf("%d\n", monster->hp()); // `80`
|
std::cout << "hp : " << monster->hp() << std::endl; // `80`
|
||||||
printf("%d\n", monster->mana()); // default value of `150`
|
std::cout << "mana : " << monster->mana() << std::endl; // default value of `150`
|
||||||
printf("%s\n", monster->name()->c_str()); // "MyMonster"
|
std::cout << "name : " << monster->name()->c_str() << std::endl; // "MyMonster"
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
*Note: That we never stored a `mana` value, so it will return the default.*
|
*Note: That we never stored a `mana` value, so it will return the default.*
|
||||||
|
|||||||
Reference in New Issue
Block a user