Several years ago I inherited a C+ (I don't think it's C++) application to maintain. Every now and then I'm asked to include a new function in it.
I know very basic C+ programming, and much of the time I re-using other parts of the program, tweeking it to do something new.
Here's my problem.
This function reads a single line, comma-separated, text file, and splits it into an array. The function is then called with a pointer to the relevant array cell, returning that value.
The thing is, the last value (pointer = "id" below) has a CR/LF included, which is problematic to the calling application.
How does one remove that pesky CR/LF?
Again, I'm not an experienced C++ programmer. The code below was copied from another part of the program, with slight changes from me.
Any help would be appreciated.
Thanks very much.
The '\n' comes probably from fgets(tstr,124,grop);
'\n' is part of the input and fgets adds the '\n' when less than required chars are read.
You can manually delete it or use your own fgets function like mine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
Like the standard fgets but removes a trailing '\n'
*/
char* my_fgets(char *str, int num, FILE * stream)
{
assert(str != NULL); /* #include <assert.h> */
assert(stream != NULL);
assert(num > 1);
char* retval = fgets(str, num, stream);
char *pos = strrchr(str, '\n');
if (pos)
*pos = '\0';
return retval;
}