c++ - Why is the beginning of my string disappearing? -


In the following C ++ code, I realized that gcount () Was wanting back, because getline () consumes the character of the new line but does not send it to the input stream.

Whatever I did not understand is the output of the program, though. "Test \ n" For input, why do I get "Est \ n"? How does my mistake affect the first character of the string instead of adding unwanted nonsense on the end? And how does the output of the program appear in the debugger ("Test \ n", as I expected), how does it come to the barriers?

  #include & lt; Fstream & gt; #include & lt; Vector & gt; #include & lt; String & gt; # Include & lt; Iostream & gt; using namespace std; Int main () {int buffer size = 1024; Ifstream input ("test.txt", iOS :: in | iOS :: binary); Of vector & lt; Four & gt; Wakebuffer (buffer size); Input.gateline (& vecBuffer [0], buffer size); String Strass (vecBuffer.begin (), vecBuffer.begin () + input.gcount ()); Cout & lt; & Lt; StrResult & lt; & Lt; "\ N"; Return 0; } I have also repeated this result, Windows Vista, Visual Studio 2005 SP2   

When I think what the heck is happening, then I will update this post.

Edit : Okay, we go there. Problems (and different results are getting people) are from \ r. What happens when you call input.getline and put the result in vecBuffer. The gateline function closes \ n strips, but leaves \ r in place.

Then you transfer the wiffer to the string variable, but use the gateout function from the input, which means that you will get a lot four because the input variable is still \ n included, and VecBuffer does not

The resulting strasselt is:

  - strResult "test" [0] 84 't' four [1] 101 'e' four [2] 115 'four' [3] 116 't' four [4] 13 '␍' four [four] [four] [four]  

Then the "test" is printed, followed by Carris Return ( Cursor back in the beginning of the line), a blank character (written on T), and finally \ n, who correctly puts the cursor on the new line.

Then you must either remove \ r, or write a function that checks the length of the string directly from the Wakebuffer, blank characters.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -