Home c++ WriteFile Function from WinAPI

WriteFile Function from WinAPI




I will clarify just in case it is about functions .
I noticed that it skips the transition symbol to another line: \ N ". How to make the row transition?


Sample code:

int main ()
  STRING PATHEXE = "C: \\ Projects \\ Tests \\ Release \\";
  Auto File = Createfilea ((Pathexe + "logtest.txt"). c_str (), generic_write, file_share_read, null, creat_always, file_attribute_normal, null);
  if (file == invalid_handle_value)
    Printf ("Error% X \ N", GetLastterror ());
    STD :: COUT & LT; & LT; "\ NRELOAD NEEDED. Log Error"; // MessageBox
  STD :: STRING TEXT = "\ N1";
  for (int i = 0; i & lt; 100; ++ i)
    WriteFile (File, text.c_str (), (text) .size (), & amp; size, null);
    Size + = (Text) .Size ();
  Return 0;

Update2: When I wrote a question, I opened the file with a regular notebook (Windows 7). Now opened NotePad ++ and there are transitions to another line Oo. How so?

Answer 1, Authority 100%

The problem is in what.

When you write a string into a file through standard streaming functions (and the file is open as text, and not as binary), transcoding occurs on the fly from the short ends of the strings (\ N ) to system-dependent ( On Windows \ R \ N ). And the ends of the rows of the type \ R \ N fall into the file (you can check any hex editor).

When you write via Writefile , he knows nothing about lines, and writes to the file as it is. That is, the ends of the rows of the type \ N fall into the file. (You can check again any hex editor.)

How correctly suggests @ograndy in the comments, notepad perceives only the ends of the row of the \ R \ N . On the other hand, in the UNIX standard, the ends of the strings have the kind of \ n , and your file is essentially inadvertently in this format. Since other editors understand this format, they interpret your file as the UNIX text, and show it as you wanted.

Solution – either add \ r before \ n in your lines, or write through standard string functions of the language.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions