Thursday, 15 September 2011

c++ - Storing variable sized strings in structures -


I am reading a file in C ++ using currents, in particular, fstream, ifstream no.

  blah blah blah blah blah blah \ n blah blah blah \ n and  

it repeats and together

  1. In Blah
  2. Continuous continuous rows between each row, finally the delimiter is

I have to read a set of data, then store it in a character array To do that, I am trying to use getline () in a style style but the delimiter can be only one letter, not B three I obviously can not just try to read a set number of bytes by using the read (), because the number will vary for each set.

So I think it's the easiest (and most strong) thing to do here. Until I can not find the 'end' string, unless each string is added up and up?

I tried a 2D four array, but I had a kind of pain copying it. Can I use strncpy here? I do not think this work

  four buffs [10] [10]; Strncpy (buf [1], "blah blah", 10);  

I have some ideas here, but I'm not sure which one is (or which I do not) is the best.

Edit: So it is for a networking application, so the size of the four array (or string) should always be the same. In addition, there should be no sign in structure

Related questions: Is there a way that four arrays and one std :: string are evenly stored in memory? I always had something upper with std :: string though.

(my utility is described below)

  type type std :: vector & lt; Std :: string & gt; Block; Int main () {namespace std; Of vector & lt; Block & gt; Block; String constant and = "end"; // There is no real difference to use ifstream (btw ("file name", file.in) for file fstream; file;) {block & amp; Block = push_back (block); (String line; for gateline (file, line);) {if (line == end) {break; } Push_back (block) .swap (line); } If (! File & Block. Meta ()) {// Do not read any line, a dummy is not represented in block block. Pop_back (); }} Return 0; }  

Example serialization:

  template & lt; Class OutIter & gt; Zero bencode_block (block const & block, outISTERST) {int len ​​= 0; (Block :: const_iterator i = block.begin (); i! = Block.end (); ++ i) {len + = i- & gt; Size () + 1; // contains newline} * dest ++ = len; * Dest ++ = ':'; (Block :: const_iterator i = block.begin (); i! = Block.end (); ++ i) {* dest ++ = * i; * Dest ++ = '\ n'; }}  

I have used a simple serial format. I Example Useful output iterator, which just writes a stream:

  struct WriteStream {std :: ostream & amp; outside; ListStream (Stud :: Ostream and Out): Out (Out) {} ListStream & amp; Operator ++ () {return * this; } ListStream & amp; Operator ++ (int) {return * this; } ListStream & amp; Operator * () {Return * This; } Template & lt; Square T & gt; Zero operator = (T const and value) {Out & lt; & Lt; value; }};  

Example usage:

  Bencode_blok (block, listStream :: code);  

Another potential output ister, which writes one (such as a network socket):

  struct WriteFD ​​{int out; WriteFD ​​(Intake Out): Out (Out) {} WriteFD ​​& amp; Operator ++ () {return * this; } WriteFD ​​& amp; Operator ++ (int) {return * this; } WriteFD ​​& amp; Operator * () {Return * This; } Template & lt; Square T & gt; Zero operator = (t const & value) {if (type (value) == -1) {std :: runtime_error (strelror (errno)); }} // Note: The types of writing do not currently write less byte compared to writing int (less value) provided {write back (out, & value, 1); } Write int (std :: string const and value) {write back (out, value.data (), value.size ()); } Int write [four buff [20]; // INT_MAX up to 9999999999999999999 / handle INT_MIN down -999999999999999999 / / that is 19 and 18 nuns, respectively (do you calculate, do not? P) int lane = sprint (buff, "% d", value); Write return (outside, buff, lane); }};  

Poor man's trick words:

  template & lt; Class C & gt; Type name C: value_type & amp; Push_back (C & container) {container.push_back (typename C :: value_type ()); Return container.back (); }  

This allows easy access to the transfer words to avoid unnecessary copies:

  container.push_back (value); // copies // becomes: // (the type of container is) container.push_back (C :: value_type ()); // Add empty container. Back (). Swap (value); // swap content  

No comments:

Post a Comment