
On Thu, 31 May 2012, Jason White <jason@jasonjgw.net> wrote:
You could write a function to do this without much trouble though.
Below is my rough analog to fgets() which takes either \r or \n as the end of a line. It works because I don't mind getting the occasional empty line in the middle of the file, programs which can't handle extra empty lines won't work with this.
int get_line(char *buf, int buf_len, FILE *fp) { int c, i = 0; while((c = fgetc(fp)) != EOF) { if(c == '\r' || c == '\n') { buf[i] = 0; return 1; } buf[i] = (char)c; i++; if(i == buf_len - 1) { buf[i] = 0; return 1; }
If you pass buf_len=0 or buf_len=1 you would get yourself a buffer overflow. Maybe that doesn't really matter in your case if you use hardcoded inputs, but I'd still check for it to save from puzzling crashes if you re-use the code in future. James