How to read file having different line ending in C++
Solution 1:
As Sebastian said, we will need to read the block and then find out the appropriate line-ending.
So, we will need to open the file in binary mode and read the last characters.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void SetLineEnding(char *filename, std::string &newline, char &delimiter)
{
std::string str;
std::ifstream chk(filename,std::ios::binary);
if(getline(chk, str))
{
if(str.size() && str[str.size()-1] == '\r')
{
//It can be either \r or \r\n
if(getline(chk, str))
{
delimiter = '\n';
newline = "\\r\\n";
}
else
{
delimiter = '\r';
newline = "\\r";
}
}
else
{
delimiter = '\n';
newline = "\\n";
}
}
}
int32_t main()
{
string newLine;
string delimiter;
char filename[256];
in>>filename;
SetLineEnding(filename,newLine,delimiter);
std::ifstream inp(filename,ios::in);
if(!inp.is_open())
{
cout<<"File not opened"<<endl;
return 0;
}
//getline() function with delimiter
string str;
getline(inp,str,delimiter);
return 0;
}
Now you can pass delimiter to getline() and you will be able to read according to line-ending.