Search code examples
c++file-handlingifstreamgetlineline-endings

How to read file having different line ending in C++


I have two files "linuxUTF8.srt" and "macANSI.srt". I am reading these files using getline(). as macANSI.srt has '\r' as line ending I am reading the whole file rather than a single line. I know I have to pass '\r' as delimiter but how do I know what type of line ending character I am dealing with.


Solution

  • 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.