我已經寫了一個函數來包裝GNU getline()並刪除尾隨的換行符,但由於某種原因它沒有效果。難道我做錯了什麼?刪除換行符的函數沒有作用?
ssize_t readline(char **lineptr, FILE *stream)
{
size_t len = 0; // Size of the buffer, ignored.
ssize_t chars = getline(lineptr, &len, stream);
if((*lineptr)[chars] == '\n') {
(*lineptr)[chars] = '\0';
--chars;
}
return chars;
}
它編譯和鏈接沒有問題,但換行符不被替換爲空終止符。我已驗證if()語句正在運行。謝謝你的幫助!
剛剛用這個去除了那個換行符, g_strchomp(line) – TechZilla