I think I know where is your problem
imagine you have text file made under windows, so end line is CR+LF. And now you will load this .ini file into your parser in windows and linux :
WINDOWS :
if you call fgets, it will detect newline as CR+LF and end character in your string is \n
You will use [length - 2] and everything is allright
LINUX :
if you call fgets, it reads until LF and string will have \r\n in the end. So you must use [length -3] to skip to section name
How to fix it :
I use something like this in
my parser to skip trailing whitespaces
Code:
#include <string>
int main()
{
std::string line = " test string \r\t\n \r \t\n ";
std::string result;
std::string::size_type j;
std::string::size_type begin = 0;
std::string::size_type end = line.size();
// skip all leading whitespaces
for (j = begin; j < line.size(); ++j) {
if (line[j] > ' ')
break;
++begin;
}
// skip all trailing whitespaces
for (j = end; j > 0; --j) {
if (line[j - 1] > ' ')
break;
--end;
}
if (end > begin)
result = line.substr (begin, end - begin);
else
result = " ";
}
However this code is for std::string class, not
evil char array
You only check for \n in your trim function. You should also check for \r, or better, check for all whitespace characters by comparing them with space, like I did in previous example
more info :
http://en.wikipedia.org/wiki/Newline