Complimenti!
Grazie
Ciaoo :)
Moderatori:
Paolino,
fairyvilje
// getbuf.c
#include <stdio.h>
#define BUF_SZ 5
#define VAL_MIN -100
#define VAL_MAX 100
int main()
{
int buf[BUF_SZ] = {0};
for (size_t i = 0; i < BUF_SZ; ++i) {
int nr = 1; // Numero di campi letti da scanf: ok quando nr == 1
do {
printf("buf[%zd] = ", i);
fflush(stdout);
if (nr < 1) // Se nr < 1 elimina i caratteri rimasti in stdin
scanf("%*[^\n] ");
} while ((nr = scanf("%d", buf+i)) < 1 || buf[i] < VAL_MIN || buf[i] > VAL_MAX);
}
return 0;
}scanf("%*[^\n] ");>getbuf
buf[0] = 1 2 3 4 5
buf[1] = buf[2] = buf[3] = buf[4] =>getbuf
buf[0] = 75.36
buf[1] = buf[1] =1
2
3>getbuf < in.txt// getbuf.c
#include <stdio.h>
#include <limits.h>
#define BUF_SZ 5
#define VAL_MIN INT_MIN
#define VAL_MAX INT_MAX
int main()
{
int buf[BUF_SZ] = {0};
for (size_t i = 0; i < BUF_SZ; ++i) {
int nr = 1; // Numero di campi letti da scanf: ok quando nr == 1
do {
printf("buf[%zd] = ", i);
fflush(stdout);
if (nr < 1) // Se nr < 1 elimina i caratteri rimasti in stdin
scanf("%*[^\n] ");
} while ((nr = scanf("%d", buf+i)) < 1 || buf[i] < VAL_MIN || buf[i] > VAL_MAX);
}
return 0;
}>getbuf
buf[0] = 222222222222222222222222222222222222222222222222222222222222222222222222
buf[1] =
instead of
(Anonimo).
ain't
, right?
in lieu of
.
for
arithm.

char *fgets(char * str, int num, FILE * stream);// getbuf.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define BUF_SZ 5
#define LINE_SZ 128
#define VAL_MIN -100
#define VAL_MAX 100
int main()
{
int buf[BUF_SZ] = {0};
char line[LINE_SZ] = {0};
size_t n_stored = 0;
while (n_stored < BUF_SZ &&
(printf("buf[%zd] = ", n_stored),
fflush(stdout),
fgets(line, LINE_SZ, stdin))) {
/* Rimuove il '\n' e tronca una linea eccedente LINE_SZ-1 caratteri */
size_t line_len = strlen(line);
if (line_len > 0) {
if (line[line_len-1] == '\n')
line[line_len-1] = 0;
else {
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
}
/* Converte la linea letta in un intero */
/* In caso di errori, non aggiorna buf e torna indietro */
char *endp;
errno = 0;
long val = strtol(line, &endp, 10);
if (*endp ||
endp == line ||
errno == ERANGE ||
val < VAL_MIN ||
val > VAL_MAX)
continue;
/* L'input è andato a buon fine, aggiorna buf */
buf[n_stored++] = val;
}
/* Per controllo, stampa gli elementi memorizzati */
printf("\n%zd elementi memorizzati\n", n_stored);
for (size_t i = 0; i < n_stored; ++i)
printf("buf[%zd] = %d\n", i, buf[i]);
return 0;
}
while (n_stored < BUF_SZ &&
(printf("buf[%zd] = ", n_stored),
fflush(stdout),
fgets(line, LINE_SZ, stdin))) {
n_stored < BUF_SZ && fgets(line, LINE_SZ, stdin) != NULL
size_t line_len = strlen(line);
if (line_len > 0) {
if (line[line_len-1] == '\n')
line[line_len-1] = 0;
else {
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
}
if (*endp ||
endp == line ||
errno == ERANGE ||
val < VAL_MIN ||
val > VAL_MAX)
instead of
(Anonimo).
ain't
, right?
in lieu of
.
for
arithm.

Visitano il forum: Nessuno e 46 ospiti