Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    [C] Liste e allocazione di stringhe

    Quando Creo una lista di stringhe... Cosa devo fare ???
    codice:
    struct linefile {
    	char *line;
    	struct linefile *next;
    };
    Alloco lo spazio per la Lista...
    codice:
    struct linefile *p;
    ...
    p = (struct __linefile *) malloc (sizeof(struct __linefile))
    ...
    Ma poi come faccio per allocare lo spazio per la stringa...
    (La stringa la devo leggere da un file)(char *line)

    Io avevo pensato...
    prima uso un buffer interno con dimensione fissa, poi con strlen() vedo quanta
    memoria occupa, e poi alloco lo spazio per il puntatore ...->line, e poi copio
    il contenuto del buffer in ...->line ???
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  2. #2
    sì.. dovrebbe essere quello il metodo.. allocando lo spazio per un elemento allochi giustamente solo lo spazio per il puntatore alla stringa.. poi crei una stringa in modo "tradizionale" e assegni il suo indirizzo all'elemento della struct.. facendo come dici tu minimizzi lo spreco di spazio


  3. #3
    Perche' mi da' segmentatio fault ???
    codice:
    #include <malloc.h>
    #include <string.h>
    #include <stdio.h>
    
    struct __linefile {
    	char *line;
    	struct __linefile *next;
    };
    
    struct __linefile *__create_linefile_list (FILE *fp) {
    	struct __linefile *paus;		/* Temp Pointer */
    	struct __linefile *p;			/* List Pointer */
    	char buf[1024];
    	
    	if (!feof(fp)) {
    		if ((p = (struct __linefile *) malloc (sizeof(struct __linefile))) == NULL) {
    			return (NULL);			   /* I Can't allocate memory for the List... */
    		}
    		
    		fgets(buf, (sizeof(buf)/sizeof(buf[0]))-1, fp);
    		if ((p->line = (char *) malloc ((1 + strlen(buf))*sizeof(char))) == NULL) {
    			return (NULL);			 /* I Can't allocate memory for the String... */
    		}
    		strcpy (p->line, buf);
    		
    		paus = p;
    		
    		while (!feof(fp)) {
    			if ((paus->next = (struct __linefile *) malloc (sizeof(struct __linefile))) == NULL) {
    				return (NULL);			   /* I Can't allocate memory for the List... */
    			}
    			paus = paus->next;
    			
    			fgets(buf, (sizeof(buf)/sizeof(buf[0]))-1, fp);
    			if ((p->line = (char *) malloc ((1 + strlen(buf))*sizeof(char))) == NULL) {
    				return (NULL);			 /* I Can't allocate memory for the String... */
    			}
    			strcpy (p->line, buf);
    		}
    		paus->next = NULL;
    		return (p);
    	}
    	
    	return (NULL);					/* Empty List... */
    }
    
    int main() {
    	struct __linefile *list;
    	FILE *fp;
    
    	
    	fp = fopen("test.c", "rt");
    	list = __create_linefile_list(fp);
    	if (list == NULL) printf ("Error");
    	fclose (fp);
    	
    	while (list != NULL) {
    		puts (list->line);
    		list = list->next;
    	}
    	return (0);
    }
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  4. #4
    la segmentation fault la da su:

    if (!feof(fp)) {



    scusa una cosa.. ma...

    fp = fopen("test.c", "rt");

    che modalità è "t"??

  5. #5
    t = text
    b = binary

    rt, rb, wt, wb, at, ab
    Non e' quello il problema... :tongue:
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  6. #6
    Ho provato a farlo io, e mi pare funzioni:

    codice:
    /* semplice lettura e scrittura da file a pila.. */
    
    #include <stdio.h>
    #include <string.h>
    
    struct elemento {
     char *elemento;
     struct elemento *punt;
    };
    
    void crea (struct elemento **);
    void aggiungi (struct elemento **, char *);
    void visualizza (struct elemento *);
    
    int main (void) {
     char *path ="michele.mf", char_tmp[480];
     struct elemento *lista;
     FILE *fp = fopen (path, "r");
     if (!fp) {
      printf ("File non trovato : %s \n", path);
      return (-1);
     }
     crea (&lista);
     while (!feof (fp)) {
      fgets (char_tmp, sizeof (char_tmp), fp);
      aggiungi (&lista, char_tmp);
     }
     visualizza (lista);
    }
    
    void crea (struct elemento **e) {
     *e = NULL;
    }
    
    void aggiungi (struct elemento **e, char *str) {
     unsigned int lunghezza;
     struct elemento *p;
     p = (struct elemento *) malloc (sizeof (struct elemento));
     lunghezza = strlen (str);
     p -> elemento = (char *) malloc (lunghezza + 1);
     strncpy (p -> elemento, str, lunghezza + 1);
     p -> punt = *e;
     *e = p;
    }
    
    void visualizza (struct elemento *e) {
     if (!e)
      printf ("Nessun elemento è presente nella pila.\n");
     else
      while (e) {
       printf ("%s", e -> elemento);
       e = e -> punt;
      }
     
    }
    Ovviamente ho usato uno stack per rendere più veloce da scrivere e più snello il codice.
    Quindi il file te lo scrive al contrario.

    Dimmi se ti è servito, ciao!

  7. #7
    C'e' un problema...L'ultimo elemento del file viene ripetuto due volte (l'ultima linea)

    cmq grazie.
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

  8. #8
    Originariamente inviato da kNemo
    C'e' un problema...L'ultimo elemento del file viene ripetuto due volte (l'ultima linea)

    cmq grazie.
    Risolto.

    codice:
    while (fgets (char_tmp, sizeof (char_tmp), fp)) {
                    printf ("%s", char_tmp);
                    aggiungi (&lista, char_tmp);
            }
    Ho sbagliato ad impostare, non occorre la feof(), ciao!

  9. #9
    Originariamente inviato da kNemo
    t = text
    b = binary

    rt, rb, wt, wb, at, ab
    Non e' quello il problema... :tongue:
    infatti èera spolo una curiosità

    conosco la modalità binatria ma la t per text non l'avevo mai usata

  10. #10
    Cosa c'e' di sbagliato nel mio sorgente ???
    PoWered by:
    Gentoo 1.5.3 - Kernel 2.6.7
    Debian Sid - Kernel 2.6.7 - Bash 3.0
    Slackware current - Kernel 2.6.7

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.