C file read by line up to a custom delimiter -
is there function in c read file custom delimiter '\n'?
for example: have:
i did write \n exemplify in file lf (line feed, '\n', 0x0a)
this firstline\n second line\n
i'd file read part , split in 2 strings:
this firstline\n second line\n
i know fgets can read num of characters not pattern. in c++ know there method in c how it?
i'll show example:
i'm reading file abc.txt
abc\n def\n ghi\n
with following code:
file* fp = fopen("abc.txt", "rt"); const int linesz = 300; char line[linesz]; char* res = fgets(line, linesz, fp); // res filled abc\ndef\nghi\n fclose(fp);
i excpected fgets had stop on abc\n
but res filled with: abc\ndef\nghi\n
solved: problem using notepad++ in windowsxp (the 1 used don't know happens on other windows) saved file different encoding.
the newline on fgets needs crlf not cr when type enter in notepad++
i opened windows notepad , worked fgets reads string abc\n on second example.
fgets()
read 1 line @ time, , include newline character in line output buffer. here's example of common usage.
#include <stdio.h> #include <string.h> int main() { char buf[1024]; while ( fgets(buf,1024,stdin) ) printf("read line %lu characters long:\n %s", strlen(buf), buf); return 0; }
but since asked using "custom" delimiter... getdelim()
allows specify different end-of-line delimiter.
Comments
Post a Comment