C++ send objects in named pipe [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i am trying to send an object throught a named pipe but, i don't understand how to serialize my object :
class Order {
public:
void addFile(std::string const &file);
void setType(Parser::Information const &type);
std::list<std::string> getFileList() const;
Parser::Information getType() const;
void clear();
private:
std::vector<std::string> fileList;
Parser::Information type;
};
i already managed to make my named pipe work with basic data types but, i don't understand how to send and receive a full object (without using boost serialization)
I tried to put the object's datas in a struct, but I didn't manage to send it through named pipes... probably because of the vector
Could someone share his knowledge with me please

You need to convert the structure into something that can fit into a single array of bytes that can be written to the pipe as a block of memory. A string is the simplest example.
A trivial (but inflexible) way to format your data as a string would be to use C++ string streams to write type, followed by a newline, then write the first entry in fileList, followed by a newline, then the second entry, a newline, etc. When the far end of the pipe receives this data, it would have to read the first line from the string, parse it into type, then read each of the next lines and add them to file list. (If your named pipe is across the network, you may want to encode the data as utf-8 to avoid character set problems.)
In practice, you want a more flexible file format that tags the values. A common solution is JSON, which can encode multiple structures, vectors, boolean, int, double and other values. JSON is always utf-8 and can handle nested structures. C++ isn't the easiest language from which to use JSON, but it's better than rolling your own solution. One library that can read and write JSON data is https://github.com/open-source-parsers/jsoncpp.
At the high end of serialization formats is binary encoding, which is much faster to parse than string-based data. However, binary data is not human readable, so it can be more difficult to debug. One example of a library that does binary encoding/decoding is Cap'n Proto at https://capnproto.org/.

Related

What's the best way to store binary

Ive recently implemented Hoffman compression in c++, if I were to store the results as binary it would take up a lot more space as each 1 and 0 is a character. Alternatively I was thinking maybe I could break the binary into sections of 8 and put characters in the text file, but that would kinda be annoying (so hopefully that can be avoided). My question here is what is the best way to store binary in a text file in terms of character efficietcy?
[To recap the comments...]
My question here is what is the best way to store binary in a text file in terms of character efficiently?
If you can store the data as-is, then do so (in other words, do not use any encoding; simply save the raw bytes).
If you need to store the data within a text file (for instance as a paragraph or as a quoted string), then you have many ways of doing so. For instance, base64 is a very common one, but there are many others.

Designing Game of Life using classes C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
for my final project in my intro C++ class we have to design a version of Game of Life using classes and file I/O. I have been given some beginning functions/instructions but can't even begin to understand where to start or if I am missing other functions to get started. I've included the instructions given and what I have so far. I don't necessarily need the whole thing laid out for me, but if I could just have a little help on how to get started, that would be great.
Instructions:Since the project is in chapter 7, the book’s version doesn’t work with classes and it doesn’t have any file IO. Let’s
address the file IO first. On the project download page in addition to the usual items there will be two data files
containing very simple life community specifications. The files have the following format: first item in the file is the
number of rows the community requires, second item in the file is the number of columns the community requires, third
item is the LIFE community which is stored as ‘.’ (dead) or ‘O’ (alive) characters in the array shape specified by the
preceding two values.
In order for your project to work with these inputs you will need to specify a two dimensional array. The book specifies a
22 by 80 array. The GTA project uses a 50 by 100 (row by col) array. So long as your array is larger than the size specified
by the input, your code will work with the input. After creating the array, the code reads in the data from the input and
fills out your LIFE community array with a small twist. The book suggests filling in the grid directly with asterisks for live
cells and blanks for dead cells. We will use class objects instead.
The normal implementation of LIFE uses two identical arrays. One stores the now generation and one is used to store
the next generation. (see the book pgs 446 & 447) We will be using one array which contains LIFE cell objects made from
the simplest useful class we could think of. Our class objects will contain two Boolean data items that store the cell’s life
condition and one function which will age the cell.
Your LIFE community’s size should be square and an edge length is define globally as const int edge=#. Your class is
named cell and contains the public boolean variables aod_d0, aod_d1 and the void function age(). Create a general
function that counts the number of living neighbors of a cell and declare its type with the following declaration: int
nbors_sum(cell[edge][edge], int, int, int, int);. Your LIFE community ages a day at a time so create a general function that
reads cells at d0 and determines whether that cell is alive or dead (aod) at d1. It’s declaration is: void oneday(cell[edge]
[edge], int, int);. The oneday function will call the nbors_sum function. The GTA version has a fair amount of code in
main() including file input and the while(true) display loop.
Code:
#include <iostream>
#include <fstream>
using namespace std;
const int edge=20;
class cell{
public:
bool aod_d0, aod_d1;
int nbors_sum(cell[edge][edge],int,int,int,int);
void oneday(cell[edge][edge],int,int);
int main()
{
ifstream in;
in.open("glidergun.txt");
if(in.fail())
{
cout <<"Input file failed to open.\n";
return 1;
}
oneday()
in.close();
return 0;
}
void age();
int nbors_sum(cell[edge][edge],int,int,int,int);
void oneday(cell[edge][edge],int,int){
}
It's not so hard - that's a pretty common excercise, so there's a lot of material on the Web. Just check Wikipedia, for example, to get an idea and see some animations of this "Game" in motion:
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
As a starting point, I would suggest that you do the following:
Make your program read in a file and store the data within a 2D
array, as suggested by the excercise
Make your program print the
resulting 2D array to the console (standard output)
If you manage to get those two steps done, by the point you proceed you will already have a better understanding of what this is all about.
My general suggestion would be that you, if you face a problem like that where you don't know what to do, just start by doing the very obvious things that need to be done anyway (such as the reading the file, in this case). By doing that, you will get familiar with the rest on the way.

write and load decision tree to file C++ [duplicate]

This question already has answers here:
How to Serialize Binary Tree
(11 answers)
Closed 6 years ago.
I have a decision tree node class defined in following way:
class dt_node
{
public:
dt_node* child[2]; //two child nodes
int feature;
double value; // feature and value this node splits on
bool leaf;
double pred; // what this node predicts if leaf node
}
Is there a way I can write this to a file and reconstruct the tree from the file if needed?
You can do it anyhow you want...
And the real answer: it really is up to you only. If I were you, and had to save this kind of object in a .txt file, I would just make up some way to save this structure, for example as 0*0*0.0*0*0.0. With the first 0 representing the number of child nodes, second 0 representing the feature value and so on, while * character being a separator between values. Spaces could work better, but I just don't like them as separators in my files... Text file would then have some other character (for example, an |) between each separated object. Example would look like 3*22*31.11*1*1.0|2*2*1.0*0*33.3.
Obviously I could've misinterpreted your qestion. If you ask is there a way of saving this particular code and execute it via opening the file in a program without the dt_node class, I, unfortunately, feel like my knowledge is not sufficent enough to answer.
Hope it helps anyhow.
If you would like to write the format yourself, I'll just write every other node's parameters in the file (two doubles, bool and one int) along with it's level starting from the root node and then recurrently proceeding through the tree. As I can see, the bool you have in it controls whether the node have or have not any children, this will help in the reading file process.
File reading will be a bit mode complex than file writing. For each node you read, recurrently, again, read next nodes until any node's level will be equal or lesser than the current node's. It sounds complex, but it really isn't.
Of course you shouldn't write the note* pointers to the file, as they contain useless information, as upon reading the file you will have to recreate the full tree again.
Adding boost to your project can be a little bit of a pain, but there's quite a few libraries there including maths and graphics, so it may well be worth the effort.
The Boost serialisation docs are here with a tutorial here
The serialisation library allows you to add even just 1 function to your class which then defines how to save and load the state of that class. How that data is actually saved is then done by the boost library, for example you can have it save with binary, xml & text.
The only thing that you need to watch out for is that the binary serialisation is not machine transferable.

deserialize data on the fly from stream or file

It is more or less quite easy to serialize and deserialze data structures with common libraries like boost::serialize.
But there is an also common case where I simply do something like ( pseudo code ):
// receiver
NetworkInputStreamSerialzer stream;
while (1) // read new data objects from stream
{
stream & data;
}
As I expect the data package must already be received complete from the network socket. If only a part of the object can be read the deserialization will fail. Especially with large data sets TCP will fragment the data.
Is there a generic way to deal with this problem? I have not found any hints to this problem in the docs from boost::serialize.
As this problem is generic to any kind of streamed data, not only for TCP based streaming but also for files where one prog sends and another receives the data, there must be a general solution but I could not find anything about.
My question is not specialized to boost. I use it only as an example.
EDIT:
Maybe some more explanation to my wording of "fragmentation":
Any kind of data, independent of the size it produces in serialized format, can be fragmented in several packages while transferred via TCP or by writing it to any kind of file. There is no kind of "atomic" write and read operation which is supported from the OS neither the serialization libraries I know.
So if reading an int from a human readable format like XML or JSON I can get the problem that I read a "11" instead of "112" if the "2" is not in the stream or file in the moment I read from it. So writing the length of the following content in a human readable format is also not a solution, because the size information itself can be corrupt while the read occurs in the moment the content string is not complete in this moment.
[Note: I get a sense from your Q, that you want a better alternative for boost::serialization for your specific case. If this doesn't answer your Q, then let me know, I shall delete it.]
Recommending to use Google Protocol Buffers from my own practical experience. Below are few advantages:
It can be used on wire (TCP etc.)
Simple grammar to write the .proto file for composing your own
messages
Cross platform & available with multiple languages
Very efficient compared to JSON & XML
Generates header & source files for handy getter, setter, serialize,
deserialize & debugging purpose
Easy to serialize & deserialize -- store to & retrieve from file
The last point is bit tricky. While storing in a file, you may have to insert the length of the message first and while retrieving, you may have to first read that length & then use read() method to read the exact number of bytes.
Above same trick you may want to use while passing on TCP. In first couple of bytes, the length can be passed. Once the length is determined, you can always collect the remaining fragmented message.

C/C++ code for rearranging .txt file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i have to make a function in C++ that gets one file as an input and as an output it has another file, that is rearranged in a specific way. In the file there are names of people,names of subject they are teaching, commented lines starting with '#' and also duplicated white spaces. The input file looks like this (before ':' are the names, after it are the subjects):
john : PA1 , PA2,OSY
#this is commented line
peter: PA1, PA2
And the output in the second file should look like this:
OSY: john
PA1: john, peter
PA2: john, peter
As you can see, the function should put one subject on each line (in alphabetical order) and then there should be ':' and after that the names of the people teaching it (also in aplhabetical order). Also all commented lines should be deleted and the same with duplicated white spaces.
I know how to delete duplicated white spaces and commented lines, but I have problem with rearranding the people and subjects. Is there any possible way to do this function without using classes?
I would be very thankful for any help or advices.
bool transform ( const char * inFile,
const char * outFile )
{
// todo
}
There are various different problems to solve. You must first identify those individual problems, then solve them one by one, and then put the pieces together. Many years ago, when I was a student at university, this approach was taught to me as "divide & conquer".
Here are the individual problems I can identify in your question:
Read a text file's contents into memory, so that you deal with strings and each line is a string.
Parse a string. Split it into substrings, so that you deal with a collection of strings (tokens). Know when a string starts with a particular character.
Create a data structure in which a sorted, unique string key relates to a list of (likewise ordered and unique) strings.
Write a series of strings to a text file.
Each of the sub-problems may well be individual separate questions on Stack Overflow. I'll give you hints for every one of them, so that you can google them or browse the archive for related questions:
Reading a text file line by line in order to end up with a series of std::string objects is best done with std::ifstream and std::getline.
Getting the first character of a std::string is easy: my_string[0], first checking if !my_string.empty(). Splitting it is more difficult. I would personally use the occasion and get started with the Boost libraries. See the Boost Tokenizer example.
The data structure you need is already there - C++ itself provides it as part of the language. The first thing which comes to mind is std::map<std::string, std::set<std::string> >.
Writing is simpler: use std::ofstream and write line by line with operator<<.
std::map and std::set are your best friends here.