+ 1

"""Solved""" How to start getchar to the start (C)

Hi, I am doing a project where I need to go over input several times. I am confused and really fustrated as I can only run over the input once using getchar(), as it goes onto the next thing after being used. Is there a way to reset the thing so that it starts at the start of the given input? I have not been able to find anything like this online and I need it to do several things. Thanks

19th Apr 2022, 7:40 AM
Cam UOR
Cam UOR - avatar
3 Answers
+ 3
You can use unget functions to go back steps but when it comes to streams be aware that data comes and goes, it isn't guaranteed to be stored someplace until you store it yourself and once you do you can iterate over your own buffer to find what you seek.
19th Apr 2022, 8:44 AM
Valen.H. ~
Valen.H. ~ - avatar
0
Lol thank you for the answer but my tutor has told me I'm not meant to be able to reiterate which is really annoying. Thanks anyway
21st Apr 2022, 2:20 AM
Cam UOR
Cam UOR - avatar
0
If your input is coming from a file like stdin = freopen(stdin, "file.txt", "r"); then you can use fseek(START) semi-safely, not sure for stream redirections like ./myprogram < file.txt > out
21st Apr 2022, 8:20 AM
Valen.H. ~
Valen.H. ~ - avatar