17177

Using get line() with multiple types of end of line characters

Question:

I am using std::getline() in the following manner:

std::fstream verify; verify.open(myURI.c_str()); std::string countingLine; if(verify.is_open()){ std::getline(verify, countingLine); std::istringstream iss(countingLine); size_t pos; // Check for the conventional myFile header. pos = iss.str().find("Time,Group,Percent,Sign,Focus"); if(pos == std::string::npos){//its not there headerChk = false; this->setStatusMessage("Invalid header for myFile file"); return 0; } // loop that does more validation iss.clear(); }

The problem is I'm coding on a mac (and some files get modified with both windows tools and apple tools). Some end of line characters are \r instead of \n, so my file string is never broken into lines. I believe there is also a third one I should be checking for. I'm having trouble finding an example of setting up the delim parameter for multiple endOfLine characters.

If someone could help with that example or a different approach that would be great. Thanks

Answer1:

std::getline() only supports one end of line character. When opening a file in text mode, the system's end of line sequences are converted into one single end of line character (\n). However, this doesn't deal with end of line character sequences from other systems. Practically, all what really needs to be done is to remove the \r character from the input which remains. The best way to remove characters is probably to create a filtering stream buffer. Here is a trivial, untested, and probably slow one (it isn't buffering which means there is virtual function call for each individual character; this is horrific; creating a buffered version isn't much harder, though):

class normalizebuf : std::streambuf { std::streambuf* sbuf_; char buffer_[1]; public: normalizebuf(std::streambuf* sbuf): sbuf_(sbuf) {} int underflow() { int c = this->sbuf_->sbumpc(); while (c == std::char_traits<char>::to_int_type('\r')) { c = this->sbuf->sbumpc(); } if (c != std::char_traits<char>::eof()) { this->buffer_[0] = std::char_traits<char>::to_char_type(c); this->setg(this->buffer_, this->buffer_, this->buffer_ + 1); } return c; };

You'd use this filter with an existing stream buffer, something like this:

std::ifstream fin("foo"); normalizebuf sbuf(fin.rdbuf()); std::istream in(&sbuf);

... and then you'd use in to read the file with all \r characters removed.

Recommend

  • windows batch file combine csv in a folder by column
  • cannot load gems in test environment
  • Implementing HMAC-SHA256 for Keybase in Javascript
  • Objective C - Create a framework for my iphone apps?
  • php show all images in directory and sort by last modified
  • Which browser have this strange user agent? (IOS device)
  • Basic defensive programming [duplicate]
  • EF 4.1 DBContext AutoDetectChangesEnabled
  • TFS - how do I sum child task hours to parent
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Trying to string.Join an IList
  • How to make R's read_csv2() recognise the text characters properly
  • Elasticsearch script query involving root and nested values
  • Symfony 2. CSRF token is invalid
  • CakePHP ACL tutorial initDB function warnings
  • Install PHP intl extension on MacOS
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Using variable in a value field in jMeter
  • Why is an OPTIONS request sent to the server?
  • Sony Xperia Z Tablet not found by adb
  • Join two tables and save into third-sql
  • Javascript convert timezone issue
  • How to model a transition system with SPIN
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • ORA-29908: missing primary invocation for ancillary operator
  • Why winpcap requires both .lib and .dll to run?
  • Numpy divide by zero. Why?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • Busy indicator not showing up in wpf window [duplicate]
  • Converting MP3 duration time