C++ writing string to file = extra bytes -


I am using c ++ to look through the count of 256 and write an ASCII representative in a file .

If I use a method to generate a 256 character string, then write that string in the file, the weight of the file is 258bytes.

  string fileString = ""; // Using the counter to attach ASCII count to string. For (int i = 0; i  

If I use the method in the file with the loop, then the file is properly 256bytes.

  // For enclosing ASCII to enclose the counter by using the counter (int i = 0; i  

Running with string here, what additional information is being written in the file from the string?

Both of these create 256 byte files:

  #include & Lt; Fstream & gt; #include & lt; String & gt; Int main (zero) {std :: ofstream file ("output.txt", std :: ios_base :: binary); Std :: string fileString; {FileString + = static_cast & lt; for Intuit = 0; I & lt; 256; i ++) Char & gt; (I); } File & lt; & Lt; FileString; }  

and:

  #include & lt; Fstream & gt; #include & lt; String & gt; Int main (zero) {std :: ofstream file ("output.txt", std :: ios_base :: binary); Std :: string fileString; For (Int i = 0; I <256; ++ i) {file  

Note, before you have an off-by-one error, because it does not have 256th ASCII characters, only when 0-255 is printed, this one is shortened around Will happen. Also, like static_cast .

If you do not open them in the form of binary, then it will add a new line to the end. My standard object is weak in the field of output, but I know that text files always end up for a new line, and it is inserting it for you, I think this implementation has been defined, Because so far I can find in the standard that "the district constructor can perform additional implementation-defined operations."

Opening as a binary, of course, removes all the bars and lets you control every detail of the file.


Regarding the concern of Altter Life, you can store 0 in the string, but the C-style strings are terminated by 0. Therefore:

  #include & lt; Cstring & gt; # Include & lt; Iostream & gt; #include & lt; String & gt; Int main (zero) {std :: string result; Results = "Apple"; Results are + = stabilized & lt; Char & gt; (0); Results + = "Pears"; Std :: cout & lt; & Lt; Results size () & lt; & Lt; "Vs" & lt; & Lt; Std :: strlen (result.c_str ()) & lt; & Lt; Std :: endl; }  

Two separate lengths will print: What is counted is one that is meaningless.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -