C question about scanf

Giorgos Keramidas keramida at hades.hell.gr
Wed Sep 15 01:39:59 EEST 1999


On Tue, Sep 14, 1999 at 06:59:03PM +0300, Batis wrote:
% 
% De mou lete,
% 
% sto Linux, me compiler ton gcc, pws mporoume na perasoume ena long integer
% ap'tin sscanf se mia variable.

Xrnsimopoieis tis fgets(), strtok() kai strtol() kai ftuveis auto to aisxro
pragma pou legetai scanf().

% Px. exoume char line[80]; opou exei to periexomeno:
% 0 123 3441234 532525 4634636 575745 (pithana kai kana \n sto telos)
% kai exoume to long lt[6];

Xwris va to exw kavei compile, mia prwtn idea eivai:

	#include <stdio.h>	/* printf() in here */
	#include <stdlib.h>	/* strtol() in here */
	#include <string.h>	/* strtok() in here */
	#include <errno.h>	/* `errno' in here  */

	function koko (void)
	{
		char line[80];
		long lt[6];
		char *token;
		int k;

		strncpy(line, 79, "0 123 3441234 532525 4634636 575745");

		token = strtok(line, " \t\n");
		if (token == NULL)
			return EXIT_FAILURE;

		errno = 0;
		lt[0] = strtol(token, NULL, 10);
		if (errno == ERANGE)
			return EXIT_FAILURE;

		for (k = 1; k < 6; k++) {
			errno = 0;
			token = strtok(NULL, " \t\n");
			if (token == NULL)
				return EXIT_FAILURE;
			lt[k] = strtol(token, NULL, 10);
			if (errno == ERANGE)
				return EXIT_FAILURE;
		}

		for (k = 0; k < 6; k++)
			printf("[%d] %ld\n", k, lt[k]);
		fflush(stdout);
	}

Mou fainetai pos prepei na douleuei kala, gia dokimase to... kai tis
*scanf() functions apla ksexna tes, opws lene ki oi K&R sto biblio
"The C Programming Language" einai apla KAKES functions.

-- 
Giorgos Keramidas, <keramida at ceid.upatras.gr>
Public PGP key at finger://keramida@diogenis.ceid.upatras.gr/
"Today is not yesterday's tomorrow, but tomorrow's yesterday."
--
====================================================================
Gia boithia (h na diagrafhte) e-mail sto majordomo at hellug.gr
Ta archives tis listas einai sto http://lists.hellug.gr/archives
prin steilete kapoia erothsh psakte mipos exei hdh apanththei.
Gia opoiodipote problima stilte e-mail ston owner-linux-greek-users at hellug.gr
====================================================================



More information about the Linux-greek-users mailing list