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';
...
}
Printable View
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';
...
}
Yes, that can work, but it is simpler to use strchr instead of strlen.Quote:
Originally Posted by mzn
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:Quote:
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.Quote:
Originally Posted by mzn