🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #9749

Re: problema con READ

#9749
From: Iago Rubio
Date: Sat, 25 May 2002 08:12
77 lines
3070 bytes
On Sat, 4 May 2002 17:24:33 +0200, "cddd" <gracias@hotmail.com> wrote:

>cuando hago un:
>------------------
>char *buffer;
>
>buffer=(char *)malloc(sizeof(char) *TAMANYO)
>-------------------
>
>y luego un:
>--------------------
>a=read(fd, buffer,sizeof(buffer));
>--------------------
>
>a me devuelve -1 y no lee del fichero
>�debo poner "read(fd,buffer,sizeof(buffer)*TAMANYO)"?

Has reservado sizeof(char) *TAMANYO 

read( fd, buffer, sizeoff(char) * TAMANYO)

>(el open del fichero (fd),  no me da problemas )
>
>Mi intencion es leer de un fichero un bloque del tama�o que yo quiera y
>colocarlo en el buffer;



Si crees que el problema es de malloc() (yo no lo creo), depura con
las librerias de depuracion de malloc().

Mira si las tienes instaladas

locate libnjamd.so

y si es as� usalas para depurar el programa

export LD_PRELOAD=libnjamd.so
gdb miprog

Te dar� informaci�n adicional sobre la signacion de memoria.
---------------------------------
Haz tu propia funcion para ver donde falla:


#define EXITO                     0
#define FALLO_ARCHIVO   1
#define FALLO_MALLOC     2

int CogeBloque(FILE *f, char* buffer, int tamanho_buffer){

 int c;                  //para coger caracter de archivo
 int i;                   //contador
 FILE *f;		//descriptor de archivo

 if ( (f=fopen("file","r"))  !=  NULL){      //si abre el archivo
   //reservamos tamanho_buffer chars
   if( ( pStr = malloc( sizeof(char) * tamanho_buffer) ) != NULL){
    //cogemos caracteres del fichero comprobando que no exceda
    //la memoria reservada
    while( ( ( c=fgetc(f) ) != EOF ) && ( i < tamanho_buffer) ){    
       //guardamos caracter al buffer
       buffer[i] = c;
       i++;
     }
   }
   else{ 
      return FALLO_MALLOC;
    }
  return EXITO;
 }
 else{    // si no abre el archivo
  return FALLO_ARCHIVO;
 }
}  // fin de la funci�n

prueba en es.comp.lenguajes.c
Technical Details
Message-ID: <8neueu8rt3gklavj6k3i3mhb5nvjkd06pl@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!213-98-107-123.uc.nombres.ttd.ES!not-for-mail
References: <ab0u0o$8m$1@nsnmrro2-gest.nuria.telefonica-data.net>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,2b079131cdd34b32
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2002-05-25 01:35:07 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!213-98-107-123.uc.nombres.ttd.ES!not-for-mail
From: Iago Rubio <famrubsan@wanadoo.es>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: problema con READ
Date: Sat, 25 May 2002 10:12:09 +0200
Organization: Wanadoo
Lines: 78
Message-ID: <8neueu8rt3gklavj6k3i3mhb5nvjkd06pl@4ax.com>
References: <ab0u0o$8m$1@nsnmrro2-gest.nuria.telefonica-data.net>
NNTP-Posting-Host: 213-98-107-123.uc.nombres.ttd.es (213.98.107.123)
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: fu-berlin.de 1022315705 27994763 213.98.107.123 (16 [64988])
X-Orig-Path: antonio.escomposlinux.org!marmolejo.escomposlinux.org!eriador.escomposlinux.org!news.audiolis.com!127.0.0.1!newsfeeder.wanadoo.es!not-for-mail
X-Newsreader: Forte Agent 1.91/32.564