Multiple line break issues at the end of a TXT file read

Source: Internet
Author: User

When using Getline () to read TXT files, do not pay attention to a little bit of a bug.

Post code:

#include <iostream>#include<string.h>#include<fstream>#include<sstream>using namespacestd;intMain () {Ifstreaminch("Aaa.txt"); strings; inti =0;  while(Getline (inch, s)) {cout<<s; Const Char*p =S.c_str (); if(*p==' /')             Break; I++; } cout<<Endl; cout<<i; return 0;}

The characteristic of Getline () is that it is the end of a line that encounters a ' \ n ', and the result is that the preceding data is stored as a string object.

Use the C_str () method to convert a string object to a C string of the const char* type, with a "." after its data.

Artificially changing the line after a TXT file causes the empty string object to be read multiple times, that is, I in the while is incremented several times.

So add a judgment, jump out in time. Multiple line breaks at the end of the file result in uncertainty over where the files end.

Multiple line break issues at the end of a read TXT file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.