Langage C erreur de segmentation
Dernière réponse : dans Programmation
Bonjour
j'aurais besoin d'un petit coup de main pour trouver une petite erreur de segmentation qui me rend dingue depuis 2 jour la.
j'ai tout essayer et je l'ai pas encore trouver voila mon code
voila mon code
j'espère que quelqu'un pourrais trouver cette erreur
merci d'avance
j'aurais besoin d'un petit coup de main pour trouver une petite erreur de segmentation qui me rend dingue depuis 2 jour la.
j'ai tout essayer et je l'ai pas encore trouver voila mon code
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
void frand(int n, char *pkey);
char fencode(char car, char KEY);
int main( int argc , char *argv[] )
{
/* Déclaration des variables */
int n, i;
char *pkey, *pCypher1, *test, *pCypher2;
FILE* fichier;
/*creation de fichier*/
fichier=fopen("secret.key","wb");
/*verification de l'argument d'entrée*/
if(argc==1){
printf("pas de phrase de passe\n");
exit(0);
}
else{
/*Mise dans une variable le nombre de caractere de la phrase passe */
n=strlen(argv[1]);
printf("longueur=%x\n",n);
/*Allocation de memoire de pkey*/
if((pkey=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation de memoire\n");
exit(0);
}
else{
/*generation de pkey*/
frand(n, pkey);
printf("pkey: %s\n",pkey);
/*allocation memoire de pCypher1*/
if((pCypher1=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
/* XOR entre pkey et le passe */
for(i=0;i<n;i++)
{
pCypher1[i] = fencode(pkey[i], argv[1][i] );
printf("pCypher1[%d] = %c\n", i, pCypher1[i]);
}
printf("pCypher1: %s\n",pCypher1);
/* Ecriture de pCypher dans le fichier secret.key */
fwrite(pCypher1, sizeof(char), n, fichier);
}
}
/* Vérification */
if((pCypher2=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
fread(pCypher2, sizeof(char), n, fichier);
fclose(fichier);
printf("pCypher2: %s\n",pCypher2);
}
/* Vérification du decodage */
if((test=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
for(i=0;i<n;i++)
{
test[i] = fencode(pCypher2[i], argv[1][i] );
printf("test[%d] = %c\n", i, test[i]);
}
printf("test: %s\n",test);
/* Comparaison entre pkey et le pkey du fichier secret.key */
if( strcmp(pkey, test) == NULL)
{
printf("vérifié avec succés");
}
else
{
printf("Echec verification");
}
}
}
free(pkey);
free(test);
free(pCypher1);
free(pCypher2);
fclose(fichier);
return EXIT_SUCCESS;
}
/* generer pkey avec la suite de Fibanacci*/
void frand(int n, char *pKey)
{
/* Definition des variables */
int i, x,y,z;
char *pkey;
y = (rand()%128)+33;
z = (rand()%128)+33;
for(i=0;i<n;i++)
{
x=y+z;
if(x>128)
{
x = x - 128;
}
pkey[i] = x;
printf("pkey[%d] = %c\n", i, pkey[i]);
z = y;
y = x;
}
}
/* Fonction XOR entre deux caracteres */
char fencode(char car, char KEY)
{
return(car^KEY);
}
voila mon code
j'espère que quelqu'un pourrais trouver cette erreur
merci d'avance
Autres pages sur : langage erreur segmentation
Lassé par la pub ? Créez un compte
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
void frand(int n, char *pkey);
char fencode(char car, char KEY);
int main( int argc , char *argv[] )
{
/* Déclaration des variables */
int n, i;
char *pkey, *pCypher1, *test, *pCypher2;
FILE* fichier;
/*creation de fichier*/
fichier=fopen("secret.key","wb");
/*verification de l'argument d'entrée*/
if(argc==1){
printf("pas de phrase de passe\n");
exit(0);
}
else{
/*Mise dans une variable le nombre de caractere de la phrase passe */
n=strlen(argv[1]);
printf("longueur=%x\n",n);
/*Allocation de memoire de pkey*/
if((pkey=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation de memoire\n");
exit(0);
}
else{
/*generation de pkey*/
frand(n, pkey);
printf("pkey: %s\n",pkey);
/*allocation memoire de pCypher1*/
if((pCypher1=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
/* XOR entre pkey et le passe */
for(i=0;i<n;i++)
{
pCypher1[i] = fencode(pkey[i], argv[1][i] );
printf("pCypher1[%d] = %c\n", i, pCypher1[i]);
}
printf("pCypher1: %s\n",pCypher1);
/* Ecriture de pCypher dans le fichier secret.key */
fwrite(pCypher1, sizeof(char), n, fichier);
}
}
/* Vérification */
if((pCypher2=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
fread(pCypher2, sizeof(char), n, fichier);
fclose(fichier);
printf("pCypher2: %s\n",pCypher2);
}
/* Vérification du decodage */
if((test=(char*)malloc(sizeof(strlen(argv[1])+1)))==NULL){
printf("erreur d'allocation memoire\n");
exit(0);
}
else{
for(i=0;i<n;i++)
{
test[i] = fencode(pCypher2[i], argv[1][i] );
printf("test[%d] = %c\n", i, test[i]);
}
printf("test: %s\n",test);
/* Comparaison entre pkey et le pkey du fichier secret.key */
if( strcmp(pkey, test) == NULL)
{
printf("vérifié avec succés");
}
else
{
printf("Echec verification");
}
}
}
free(pkey);
free(test);
free(pCypher1);
free(pCypher2);
fclose(fichier);
system("PAUSE");
getchar();
return (0);
}
/* generer pkey avec la suite de Fibanacci*/
void frand(int n, char *pKey)
{
/* Definition des variables */
int i, x,y,z;
char *pkey;
y = (rand()%128)+33;
z = (rand()%128)+33;
for(i=0;i<n;i++)
{
x=y+z;
if(x>128)
{
x = x - 128;
}
pkey[i] = x;
printf("pkey[%d] = %c\n", i, pkey[i]);
z = y;
y = x;
}
}
/* Fonction XOR entre deux caracteres */
char fencode(char car, char KEY)
{
return(car^KEY);
}
voila
j'ai toujours le meme problème
des erreur de segmentation dans le frand mais je trouve pas la ligne de l'erreur
Lassé par la pub ? Créez un compte
- Contenus similaires :
Tags :
- ForumLangage c execution et erreur de segmentation
- ForumC qt probleme erreur de segmentation
- ForumErreur de segmentation, sous linux gentoo
- ForumForemost erreur de segmentation
- ForumProbleme polymorphisme erreur de segmentation
- ForumErreur segmentation c
- ForumPostgres erreur de segmentation
- ForumErreur de segmentation thunderbird, firefox
- solutionsErreur de segmentation
- ForumErreur segmentation dans boucle while
- Voir plus