I have a vector defined as follows:
std::vector<char*> rooms;
const int rows = 100;
const int columns = 10;
I need to send this vector as string buffer. And then receive and turn the string into another vector with same size.
This is how I send a string:
int SendData(std::string data)
{
s.SendLine(data);
return 0;
}
I just can't imagine a solution for that.
Since you already know how to send a string, and the vector is just a series of strings, then the most straightforward way to send the vector would be to simply send the strings, one after another.
Of course the devil is in the details: How will the receiver know how many strings to expect? And how will the receiver know where one string ends and the next string begins?
Answers: Before sending the first string, send some initial data indicating how many strings you intend to send. (If you want to keep things simple, this could be an additional string like "100" that the receiver will know to use as a hint; or you could send an integer as one or two or four bytes of binary data, if you prefer to do it that way [be sure to consider endianness issues if you send it as more than one byte])
As for the receiver knowing where one string ends and the next one begins, one way is to make sure to send a NUL/0 byte at the end of each string; then the receiver can look for that byte to know where each string ends. Another way is to send the number of bytes in the string before sending the string itself, and then the receiver can be sure to read in that many bytes for the string.
You say you know how to send a std::string, and that you want to send the vector as a single string. So all you need is to transform a vector of strings in a big string.
You can try to reinvent a way, or just use (or mimic) what others have already imagined :
Comma Separated Values
JSON
XML
All have their pros and cons. And the tricky part in all is the escaping of chars that are "special".
I need to send this vector as string buffer. And then receive and turn the string into another vector with same size.
This sounds that you solution to build a string from the data is a solution and your question goes to the point, where you want YOUR solution comes working. But there is a better way to transfer any kind of data without using manual coded string gymnastics.
If you only want to sent any kind of data like skalars, containers and custom build class instances over a network, you should think of using a library which do this for you without reinventing the wheel again and again. If this is your case search for serializer and you will get a ready to use solution for all kind of data!
Have a look at http://www.boost.org/doc/libs/1_58_0/libs/serialization/doc/index.html
or https://developers.google.com/protocol-buffers/
Related
I have not been successful on searching for this topic. I want to pass an array of strings to a C++ console app. The closest I have found is using argv(), but the number (variable) may be 50 strings which would be ugly on the calling side.
Is it possible to pass an array, or a structure to main()? I am totally open to which way to go, I have almost no experience with interprocess communication.
The conventional approach is just STDIN, as then you can send in whatever using pipes or redirection. As in: program < input
The second option is your first argument is a file to read this data from. As in program input.file
There are conventions that accommodate both, like where - as a filename is presumed to mean "read STDIN", or where no filename given means read from STDIN (e.g. grep), so you can have it both ways.
If your strings contain newlines which complicate framing you may want to use a format like INI, JSON, or YAML to read in the data.
What is the best data structures for storing
strings separated by blanks
the variable peers contains a list of strings separated by blanks
Each such a string has the form host:port, where host is a host name (DNS name or IP in dotted decimal notation2) and port is a positive number.
in the list i may have something like
127.0.0.1:8080 127.3.0.1:9080 127.0.0.1:3000 127.3.0.1:9080
what's the best way to deal with these kind of data,
any code example for
create this variable "peers"
#include <sstream>
#include <vector>
#include <string>
std::stringstream iss { str };
std::vector<std::string> peers;
std::string tmp;
while (iss >> tmp)
peers.push_back(tmp);
Assuming that there are only correct IP addresses and port numbers in the individual parts, they are no longer than 21 characters and the data could therefore be saved as an array of (fixed-length) strings:
char peers[][21] = {"127.0.0.1:8080", "127.3.0.1:9080", "127.0.0.1:3000", "127.3.0.1:9080"};
The advantage of this variant would be that the entries can be accessed directly via an array index:
for (int i=0; i<4; i++)
printf("%s\n", peers[i]);
What is the best data structures for storing strings separated by
blanks
It all depends how you are going to access this data. Are you going call it by some sort of ID? If yes, you might use map for sorted data or unordered map without sorting it. Are you going to access elements using id? Then maybe vector will suffice. Is number of string known before you start? Think about using array. About access, you can keep all the data inside one string and simply know which blank is which IP. You can also parse strings and save them into array/vector. If you want to access data not by ID but by it's value (while making sure it's inside container) make key it's value and use set. If you want to keep these inside container, don't access by id, you don't know how many items you will hold, might use list or forward_list. It's all up to how data will be processed.
Is there something like fgetc in c++? File contains 3 words "send more money" and I need to solve a task where each letter represents a number(money is the answer(:D) and I add send+more), thats why I want to read each string as char array, but now I'm struggling to do so, the only info that I got is how to read everything between spaces. So, is there something like fgetc was in c or should I rethink how to do this task?
(sorry if I will be slow to respond have to go to sleep)
There's always std::istream::get(), which works exactly like
fgetc. More idiomatic in C++ might be to use
std::istream::get(char&), which stores the results directly
into a char (rather than returning an int, which must be
checked for EOF before converting it to a char), and
signals end of file or an error in the usual way.
istream/fstream have a get function that will return characters
I have a string containing comma separated numbers (they're float, but I can convert to int). I want to send this string as data in a cURL POST request, but unfortunately sometimes the string can be too long to be sent so my program crashes. For this reason I want to condense the string into a shorter form before sending it. The fact that it is using char bytes to only store numbers from 0-9 plus commas plus (maybe) a decimal mark makes it fertile ground for compression, right?
I thought of writing something that would do the job but then decided to search for some libraries. I tried using zlib but couldn't crack it, same with a couple others, but I'm starting to think my task isn't complicated enough to use these advanced libraries, especially since I want the data to be in string format (not binary) so I can include it in a URL header. Surely there can be an easy way of doing this, no?
Here is my input sample:
"1, 2, 4, 2, 3.421, 3, 4.54, 3.43"
But for thousands of numbers.
I was thinking something along the lines of combining each couple of consecutive numbers by encoding them in one char.
I am building a distributed messaging system for users to send messages to each other on different linux terminals. The scenario that I am looking at for my question is when I try to write a string to a text file that is over 4095 bytes/chars. This string will be generated from user input. I have read that the ISO C99 standard for max string size is 4095 bytes/chars. Should I limit the user to only inputting a string that is less than 4095 bytes? I know simple chat messages are usually short but the scenario that I am concerned with is the following.
The user tries to copy and paste an excerpt from somewhere and then send it to another user. If the message was longer than 4095 bytes then it would truncate the chars after the 4095th char. Thus the user would only receive the first portion.
I’m not sure about this but I was wondering I should take the route of increasing my stack size if the string is longer than 4095 chars or is there another way around this by somehow splitting up the string as i take it in and then writing multiple strings to the file piece by piece.
That limit is on string literals, like "hello world". It's not a general limit on strings you construct programatically. You can make strings much, much larger than 4095 bytes!
Use what's called a "buffer" and write 4095 bytes at a time. I'm not a C programmer so I can't think of the buffered output function right now, but it exists.