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'; ... }
This is a discussion on excluding characters within the C Programming forums, part of the General Programming Boards category; fgets stops when it sees newline and stores it, so I think you need simply something like this: Code: size_t ...
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
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way