Read a file line by line: Difference between revisions
Revert 25 March 2020 edit affecting C language
(Add Standard ML version) |
Autoencoder (talk | contribs) (Revert 25 March 2020 edit affecting C language) |
||
Line 957:
=={{header|C}}==
===with fgets===
<syntaxhighlight lang="c">/*
* Read (and write) the standard input file
* line-by-line. This version is for ASCII
* encoded text files.
*/
#include <stdio.h>
/*
int main()▼
* BUFSIZE is a max size of line plus 1.
*▼
* It would be nice to dynamically allocate bigger buffer for longer lines etc.
* - but this example is as simple as possible. Dynamic buffer allocation from
* the heap may not be a good idea as it seems, because it can cause memory
* segmentation in embeded systems.
*/
#define BUFSIZE 1024
▲int main(void)
{
/*
* Never use gets() instead fgets(), because gets()
* is a really unsafe function.
while (fgets(buffer, BUFSIZE, stdin))
▲ else
return 0;
|