C++

Alexios Chouchoulas alexios at vennea.demon.co.uk
Thu Nov 27 13:35:40 EET 1997


On Wed, 26 Nov 1997, Tsakiris Yannis wrote:

> struct one_line
> {
> 	char *data;
> 	struct one_line *next;
> };
[...]

  Wp... Sa na periplekontai ta pragmata! :-)  Na kati pio aplo.

#include <sys/stat.h>
#include <unistd.h>

char *
readfile(char *fname)
{
	struct stat st;
	FILE *fp;
	static char *buf;

	/* Pairnoume to stat record tou arxeiou */
	if(stat(fname,&st)){
		fprintf(stderr,"Can't stat %s (errno=%d)\n",fname,errno);
		return NULL;
	}

	/* Dhmiourgoume ton buffer me to katallhlo megethos (st.st_size) */
	if((buf=(char*)malloc(st.st_size))==NULL){
		fprintf(stderr,"Can't malloc(%d)\n",st.st_size);
		return NULL;
	}

	/* Anoigoume to arxeio */
	if((fp=fopen(fname,"r"))==NULL){
		fprintf(stderr,"Can't fopen(\"%s\",\"r\"),errno=%d\n",
			fname,errno);
		free(buf);
		return NULL;
	}
	
	/* Diabazoume to arxeio */
	if(fread(buf,st.st_size,1,fp)!=1){
		fprintf(stderr,"Can't fread() from file(errno=%d)\n",errno);
		fclose(fp);
		free(buf);
		return NULL;
	}

	fclose(fp);
	return buf;
}


  Epishs mporeis na diabaseis to arxeio ligo ligo, an to protimas (an kai to
parapanw den exei diafora kai afhnei to leitourgiko na kanei olh th brwmikh
douleia). Diabazeis grammh grammh h block-block kai xrhsimopoieis thn
realloc() (h malloc() kai free() -- den empistevomai thn realloc()) gia na
megalwneis ton buffer sou stadiaka.

  A, twra pou to thymithika. Yparxei kai mia satanika low-level synarthsh, h
mmap() pou kanei paromoia douleia alla xwris na xreiazetai na diabaseis to
arxeio klp. Toulaxiston PISTEVW oti kanei thn idia douleia. Den thn exw
dokimasei pote epeidh akribws einai satanika low-level. :-)


Alexios Chouchoulas         '88    ,88'   ,88'   http://www.vennea.demon.co.uk 
The Unpronounceable One    ,o88oooo88ooooo88oo,     alexios at vennea.demon.co.uk

--
====================================================================
Gia na mathete pos na xrisimopoiite ton majordomo, stilte e-mail
sto "majordomo at argos.hol.gr" me 1 grammi sto keimeno: help
Ta archives tis listas einai sto http://www.argos.hol.gr/lists
Gia opoiodipote problima stilte  e-mail ston owner-linux-greek-users
====================================================================



More information about the Linux-greek-users mailing list