Read a file line by line: Difference between revisions

Revert 25 March 2020‎ edit affecting C language
(Add Standard ML version)
(Revert 25 March 2020‎ edit affecting C language)
Line 957:
 
=={{header|C}}==
===with fgets===
<syntaxhighlight lang="c">/* Programa: Número mayor de tres números introducidos (Solución 1) */
<syntaxhighlight lang="c">/*
 
* Read (and write) the standard input file
#include <conio.h>
* 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)
{
intstatic n1,char n2, n3buffer[BUFSIZE];
else
 
/*
printf( "\n Introduzca el primer n%cmero (entero): ", 163 );
* Never use gets() instead fgets(), because gets()
scanf( "%d", &n1 );
* is a really unsafe function.
printf( "\n Introduzca el segundo n%cmero (entero): ", 163 );
scanf( "%d", &n2 );*/
while (fgets(buffer, BUFSIZE, stdin))
printf( "\n Introduzca el tercer n%cmero (entero): ", 163 );
scanf( "%d", &n3 puts(buffer);
 
if ( n1 >= n2 && n1 >= n3 )
printf( "\n %d es el mayor.", n1 );
else
 
if ( n2 > n3 )
printf( "\n %d es el mayor.", n2 );
else
printf( "\n %d es el mayor.", n3 );
getch(); /* Pausa */
 
return 0;
2

edits