Since you are including <stdio.h>
you are implicitly allowing yourself to use the standard IO functions (If even this is forbidden, how would input or output happen in your program?).
Be sure to look into some C reference about the C standard library functions. Read the documentation of every function that you are using. Later, check also the C11 standard n1570.
On Linux, getline
is declared in <stdio.h>
but is not a standard IO function. It is in POSIX. That is documented in the getline(3)
manual page (obtained with man getline
command).
So you can, and you probably should, use functions like fopen
and fgets
and fgetc
. On Linux, see also their man page, e.g. fgets(3)
, which reminds you that it is standardized in C99.
A simple approach would be to declare some buffer for your line, e.g.
char mylinebuf[128];
to document that you accept only lines smaller than 128 bytes, and to use
char* res = fgets(mylinebuf, sizeof(mylinebuf), fp);
Don't forget to test against failure of standard IO functions. In particular, fopen
could fail and fgets
could also fail.
A more clever approach might mimic what getline
is doing (so perhaps write your own mygetline
function), using only fgetc(3)
.
If you are forbidden to use strstr
you could write your own mystrstr
function doing the same (perhaps using strchr
if you are allowed to use that, otherwise coding all by yourself). This is basic pointer stuff.
Writing your mystrstr
above strchr
is left as an exercise to the reader. And also writing your mystrchr
using basic pointer operations.
If using GCC on Linux, always enable all warnings and debug info, so compile with gcc -Wall -Wextra -g
.
Read also How to debug small programs. So on Linux learn to use the gdb
debugger and valgrind. Avoid undefined behavior -UB- (and your program have several of them, e.g. when invoked without additional arguments). For example
printf("error\n %d", fp);
is wrong and UB (and the compiler would have warned you with gcc -Wall -Wextra -g
).
You need to improve your code to avoid UB, and that is much more than just changing it to avoid strstr
.
BTW, in 2019, UTF-8 is used everywhere (in practice), and that might add complication to your program, if you want to be serious (you might want to use libunistring if you are allowed to, or else write your mystrstr
to know about UTF-8, and check that your input file contents -or your program arguments- are valid UTF-8; it might not be the case.).