Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 16

Discussione: [C] Accodare stringhe

  1. #1

    [C] Accodare stringhe

    vorrei accodare due stringhe

    oltre al canonico modo con sprintf

    volevo sapere se c'è qualcosa di + veloce di sprintf (a parte l'asm)

    questo...andrebbe bene?

    mettendo che tmpbuffer è quello che devo copiare dentrp buffer
    il tutto sta in un ciclo

    codice:
    while(....) {
      while(*tmpbuffer) {
        count++;
        *buffer = *tmpbuffer;
        tmpbuffer++
        buffer++
      }
    }
    
    buffer -= count;
    solo che quando faccio il free dei due puntatori ad eliminare i puntatori mi da seg fault :\

    xo se ad es faccio un
    printf("%s\n", buffer);

    mi stampa tutto xfettamente

    qualcuno ha idea del xche?
    (mi da seg fault eliminando tutti e due i puntatori, sia buffer, sia tmpbuffer (che non riporto alla posizione iniziale))

    idee?

  2. #2
    appena risolto

    ho almeno cosi sembra, mi sa che non mi posizionavo giusto ^^

    qualcuno potrebbe darmi delle dritte?

    codice:
    #include <gcrypt.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #define	TMPBUFFER_SIZE		1024
    
    int main(int argc, char **argv) {
    	FILE *fd;
    	struct stat file_info;
    	
    	char * buffer;
    	char * tmpbuffer;
    	
    	int res, count=0;
    	
    	// Controllo i parametri passati
    	if (argc < 2) {
    		fprintf(stderr, "Usage: %s <file>\n", *argv);
    		return 1;
    	}
    	
    	// Leggo le informazioni sul file	
    	res = stat(argv[1], &file_info);
    	if(res < 0) {
    		fprintf(stderr, "Unable to stat file %s\n", argv[1]);
    		return 1;
    	}
    
    	// Apro il file
    	fd = fopen(argv[1], "r");
    	if (fd == NULL) {
    		fprintf(stderr, "Couldn't open file!\n");
    		return 1;
    	}
    	
    	// Imposto il buffer per il file
    	buffer = malloc(file_info.st_size+1);
    	if (buffer == NULL) {
    		fprintf(stderr, "No memory avaiable!\n");
    		return 1;
    	}
    	
    	// Imposto il buffer per i blocchi di memoria temporanea
    	tmpbuffer = malloc(TMPBUFFER_SIZE+1);
    	if (tmpbuffer == NULL) {
    		fprintf(stderr, "No memory avaiable!\n");
    		free(buffer);
    		return 1;
    	}
    	
    	while((res=fread(tmpbuffer, sizeof(char), TMPBUFFER_SIZE, fd)) > 0) {
    		while(*tmpbuffer) {
    			*buffer = *tmpbuffer;
    			buffer++;
    			tmpbuffer++;
    		}
    		
    		count += res;
    	}
    	
    	tmpbuffer -= res;
    	buffer -= count;
    	
    	printf("START\n%s\nEND\n", buffer);
    	fclose(fd);
    	
    	// Elimino i buffer di memoria
    	free (buffer);
    	free (tmpbuffer);
    }
    corretto, pensato male...ci sono bufale ... ecc ecc ecc

  3. #3
    Utente di HTML.it L'avatar di pixer
    Registrato dal
    Oct 2000
    Messaggi
    614
    un semplice strcat no, eh?
    MySQL Worker - The OpenSource Multiplatform MySQL database Administrator (C++ powered)
    .:[ It resumes the development !! ]:.


  4. #4
    Originariamente inviato da pixer
    un semplice strcat no, eh?


    i sorgenti di strcat dove li trovo?

    nella libreria standard ci sono no?

  5. #5
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    tranquillo, è string.h
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  6. #6
    Originariamente inviato da pdpmpd
    tranquillo, è string.h

  7. #7
    in effetti c'è...solo che è in asm...ok usiamo quello che è + performante :sexpulp:

    ---

    codice:
    #include <gcrypt.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #define	TMPBUFFER_SIZE		1024
    
    int main(int argc, char **argv) {
    	FILE *fd;
    	struct stat file_info;
    	
    	char * buffer;
    	char * tmpbuffer;
    	
    	int res, count=0;
    	
    	// Controllo i parametri passati
    	if (argc < 2) {
    		fprintf(stderr, "Usage: %s <file>\n", *argv);
    		return 1;
    	}
    	
    	// Leggo le informazioni sul file	
    	res = stat(argv[1], &file_info);
    	if(res < 0) {
    		fprintf(stderr, "Unable to stat file %s\n", argv[1]);
    		return 1;
    	}
    
    	// Apro il file
    	fd = fopen(argv[1], "r");
    	if (fd == NULL) {
    		fprintf(stderr, "Couldn't open file!\n");
    		return 1;
    	}
    	
    	// Imposto il buffer per il file
    	buffer = malloc(file_info.st_size+1);
    	if (buffer == NULL) {
    		fprintf(stderr, "No memory avaiable!\n");
    		return 1;
    	}
    	
    	// Imposto il buffer per i blocchi di memoria temporanea
    	tmpbuffer = malloc(TMPBUFFER_SIZE+1);
    	if (tmpbuffer == NULL) {
    		fprintf(stderr, "No memory avaiable!\n");
    		free(buffer);
    		return 1;
    	}
    	
    	while((res=fread(tmpbuffer, sizeof(char), TMPBUFFER_SIZE, fd)) > 0) {
    
    		/*		
    		while(*tmpbuffer) {
    			*buffer = *tmpbuffer;
    			buffer++;
    			tmpbuffer++;
    		}
    		
    		count += res;
    		*/
    		
    		strcat(buffer, tmpbuffer);
    	}
    	free(tmpbuffer);
    	
    	printf("START\n%s\nEND\n", buffer);
    	fclose(fd);
    	
    	// Elimino i buffer di memoria
    	printf("1\n");
    	free (buffer);
    	printf("1\n");
    }
    mi da segfault quando faccio il free di tmpbuffer
    che faccio?

  8. #8
    risolto :metallica

    dovevo usare strncat

  9. #9
    mi sa che ho fatto 1 po troppi casini con i puntatori

    la bash è diventato un casino e su mozilla, non ho capito perché, non mi vanno + le freccie dx\sx

  10. #10
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

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 © 2024 vBulletin Solutions, Inc. All rights reserved.