fgets stops when it sees newline and stores it, so I think you need simply something like this:
Code:size_t len; while(fgets(buffer, sizeof buffer, f)){ len = strlen(buffer); if(buffer[len-1] == '\n') bufferl[--len] = '\0'; ... }
fgets stops when it sees newline and stores it, so I think you need simply something like this:
Code:size_t len; while(fgets(buffer, sizeof buffer, f)){ len = strlen(buffer); if(buffer[len-1] == '\n') bufferl[--len] = '\0'; ... }
Last edited by mzn; 07-08-2010 at 11:24 AM.
Yes, that can work, but it is simpler to use strchr instead of strlen.Originally Posted by mzn
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It is less work, both in the code and in what you have to consider, i.e., whether array out of bounds access is possible. For example:Originally Posted by mzn
Code:while (fgets(buffer, sizeof buffer, f)) { char *p = strchr(buffer, '\n'); if (p) *p = '\0'; /* ... */ }Both are true: the length of the string may be useful, but it may also be unnecessary.Originally Posted by mzn
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)