frankfurt_schoolgirl [she/her]@hexbear.nettoProgramming@programming.dev•How do I handle an input thats more than the char size in c?English
1·
1 year agoIf you want to accept a user input of any length, you have to read the input piece by piece and allocate a new buffer if the original becomes full. Basic steps would be:
- Use
malloc
to make achar *
buffer - Read one character at a time in a while loop, keep track of your position in the buffer
- If you get an EOF character, add a
\0
to your buffer and break the loop. You’re done! - If the position is greater than the length, allocate a new buffer that has twice the length. Use
memcpy
to copy the stuff from the old buffer to the new one. Usefree
to get rid of the old buffer.
This will work until you fill the entire memory of your computer. You should probably set a max length and print an error if it is reached.
Maybe you could just try a different Transmission docker image or build your own? Sounds like some weird instability in that particular version.