How to read file having different line ending in Cpp

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.
Double posted, also here with code.
https://www.cplusplus.com/forum/general/281858/
Yeah! that's me too.
There is an issue with that code too.
Can you provide some other implementation
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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";
        }
    }
}

Now you can pass delimiter to getline() and you will be able to read according to line-ending.
Last edited on
Pick ONE thread and stick to it.
Randomly posting between the two will just piss people off.
Read the whole file into a string. Then find the first occurrence in the string of /n or /r using .find(). Then you know which to use. Create an istringstream from the file string and then use getline() on the istringstream with the required delimiter.

The issue you have is if you have a termination of both \n and \r. If you have this, then you'll need to remove one. Choose the one that occurs last. Then remove the other from the end of the obtained string.
Topic archived. No new replies allowed.