# Reading whole lines using `fgets`

`scanf` focuses on space separated data:

:::{literalinclude} ../code-wi/reading_whole_line_scanf.c
:language: c
:::
:::{command-output} echo Anastasia Nikolaevna Romana | code-wi/reading_whole_line_scanf.exe
:shell:
:::

We see in the command output that only the first word is read into the variable, but not the rest.

`fgets` *gets* a whole line from a *file*. The file to read keyboard input is *stdin*. It additionally needs the maximum number of characters to read.

`fgets` also stores the trailing newline in the end of a line. So we have to get rid of it (compared to `scanf`).

:::{literalinclude} ../code-wi/reading_whole_line_fgets.c
:language: c
:::
:::{command-output} echo Anastasia Nikolaevna Romana | code-wi/reading_whole_line_fgets.exe
:shell:
:::

:::{tip}
Use `fgets` for reading a whole line and `scanf` for parsing word/s from a line.
:::

:::{activity}
Would you use `scanf` or `fgets` in {ref}`spare-parts-inventory-assistant`? Why?
:::

## Going back to the problem

:::{activity}
:label: spare-parts-inventory-assistant-code
Now use what you have learned to implement {ref}`spare-parts-inventory-assistant`:

- [Separation of data and program logic](project:#two-different-programming-styles).
- arrays
- using `for` loops on arrays
- string comparison
- `while`
- `fgets`
:::

## Appendix

- [Beginners guide away from `scanf`](https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html)
- [Unterstanding `fgets` and `scanf`](https://notes.iamdev.in/understanding-fgets-and-scanf-in-c/)