whereby buffer is a space in memory that got its data from a file
when I do this, and cout << ptr, I get an extra space character where the \0 should be, and strlen(ptr) shows that extra character is in the string, what's wrong?
purpose is to first break the string up into the token delimited by a space first and then I want the rest of the c string but it seems as though a space is appended where the '\0' should be which i do not want.
EDIT: ok when I read the file using getline member function and strlen(buffer) it seems that the extra space is present as well so I'm guessing that the extra space has to do with reading the file now...but i thought that getline discards the delimiting character of '\n'?
how do I solve this?
there are 26 characters in a line in the file but somehow the buffer ends up with 27 after I use getline
Using getLine() on the iostream shouldn't load the \n. However, if your loading it into a char buffer, then I'd expect strlen() to count the \n as a valid char.
Why not using getLine() to load it into a string instead? Then use the string methods to split the line. This prevents you from using the C style of character arrays.
I have to use c style strings its part of the assignment
so what should i do to solve the problem?
the program does not seem to extract the newline and discard it, in fact it is stored in the buffer, i did something like this,
char buffer50; // with square brackets
fstream file("text.txt", ios::in );
file.getline(buffer, 50);
the getline stores an extra space charcter