Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    46

    Array si stringhe [C]

    Mi dite che c'è di sbagliato in questo codice????
    Devo leggere da un file blocchi di 64 byte e metterli
    in un array di stringhe.
    Quando dopo nel for verifico se sono stati
    letti correttamente va in Segmentation fault.

    void cryptText(char *filename,mpz_t *cryptedText,mpz_t e,mpz_t n) {

    int in,nBlocchi=0,i,j,nRead;
    char *buffer=NULL;
    char **blocchi=NULL;
    mpz_t temp,pText;

    mpz_init(temp);
    mpz_init(pText);

    if ((in = open(filename, O_RDONLY))<0) {
    printf("Errore: Impossibile aprire il file %s per la lettura\n", filename);
    exit(1);
    }
    char *buffer=NULL;
    char **blocchi=NULL;

    buffer=(char *)malloc(DIMBLOCCO);
    blocchi=(char **)malloc(sizeof(char *));

    while((nRead=read(in, buffer, DIMBLOCCO))==DIMBLOCCO) {
    blocchi[nBlocchi]=(char *)malloc(nRead);
    strncpy(blocchi[nBlocchi],buffer,nRead);
    nBlocchi++;
    }

    // blocchi[nBlocchi]=(char *)malloc(nRead*sizeof(char));
    // strncpy(blocchi[nBlocchi],buffer,nRead);

    //cryptedText=(mpz_t*)malloc(nBlocchi*sizeof(mpz_t)) ;

    for(i=0;i<nBlocchi;i++) {
    //mpz_set(pText,zero);
    printf("%s\n",blocchi[i]);
    //mpz_set_ui(temp,(int)blocchi[i][j]);
    //mpz_mul_2exp(pText,pText,8);
    //mpz_ior(pText,pText,temp);
    //
    //mpz_powm(cryptedText[i],pText,e,n);
    }
    close(in);
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    46
    Possibile che nessuno può aiutarmi???

  3. #3
    Originariamente inviato da gabmar82
    Possibile che nessuno può aiutarmi???
    Ci provo

    Penzo che devi aprire un file di tipo ASCII giusto ?

    codice:
    #include <stdio>
    #include <iostream>
    #include <io>	
    #include <fcntl>
    #include <conio>
    
    //void cryptText(char *filename,mpz_t *cryptedText,mpz_t e,mpz_t n) 
    int cryptText(char *filename)
    {
    	int in, nBlocchi=0, i, j, nRead;
    	char ch;
    	char *buffer = NULL;
    	char **blocchi = NULL;
    	//mpz_t temp,pText;
    	//mpz_init(temp);
    	//mpz_init(pText);
    	
    	int DIMBLOCCO = 64+1; // Un char in più per il terminatore di stringa
    	
    	if ( (in = open(filename, O_RDONLY)) <0 )
    	{ 
    		printf("Errore: Impossibile aprire il file %s per la lettura\n", filename); 
    		//exit(1); 
    		return -1;
    	} 
    	//char *buffer=NULL;		// Perchè lo definisci un'altra volta ?
    	//char **blocchi=NULL;	// Anche questo lo definisci 2 volte ?
    
    	buffer  = (char *) malloc(DIMBLOCCO); 
    	//blocchi = (char **)malloc(sizeof(char *)); 
    	blocchi = (char **)malloc(DIMBLOCCO); 
    	
    
    	// while( (nRead=read(in, buffer, DIMBLOCCO) ) == DIMBLOCCO) 
    	// while( (nRead = read(in, buffer, DIMBLOCCO)) > 0  ) 
    	while( (nRead = read(in, buffer, DIMBLOCCO)) > 0  ) 
    	{
    		buffer[ nRead ] = '\0'; // Inserisco terminatore di stringa
    		nRead++; // La lunghezza è cresciuta +1 !
    		blocchi[nBlocchi] = (char *) malloc(nRead);
    		strncpy( blocchi[nBlocchi], buffer, nRead);
    		nBlocchi++;
    	} 
    
    	// blocchi[nBlocchi]=(char *)malloc(nRead*sizeof(char)); 
    	// strncpy(blocchi[nBlocchi],buffer,nRead); 
    
    	// cryptedText=(mpz_t*)malloc(nBlocchi*sizeof(mpz_t)); 
    
    	printf("\n\nMostro stringhe\n\n");
    	for(i=0; i<nBlocchi; i++) 
    	{
    		//mpz_set(pText,zero); 
    		printf("[%i] %s\n", i, blocchi[i]); 
    		//mpz_set_ui(temp,(int)blocchi[i][j]); 
    		//mpz_mul_2exp(pText,pText,8); 
    		//mpz_ior(pText,pText,temp); 
    		// 
    		//mpz_powm(cryptedText[i],pText,e,n); 
    	} 
    
    	close(in); 
    	return 0;
    }
    
    int main()
    {
    	cryptText("ArrayDiStringhe.cpp");
    	getch();
    }
    Ho 'commentato' le righe di codice di cui non conosco il
    funzionamento ( mpz_t & mpz_init() ).
    L'ho leggermente modificato e così a me funziona (ho usato
    il Borland c++ compiler).
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

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.