Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

struct nouedls
{
    char * path;
    char * name;
    struct nouedls * suiv;
};

typedef struct nouedls nouedls;
typedef nouedls * listes;

listes ajouters(listes ls, char * path , char* name)
{

    nouedls * x=malloc(sizeof(x));
    listes auxl;
    x->path=malloc(strlen(path)*sizeof(char));

    x->name=malloc(strlen(name)*sizeof(char));


    strcpy(x->path,path);

    strcpy(x->name,name);


    x->suiv=NULL;


    if (ls==NULL){

            ls=x;
            }
    else
    {
        auxl=ls;
        while(auxl->suiv!=NULL)
        {
            auxl=auxl->suiv;
        }
        auxl->suiv=x;

    }
    return(ls);
}

void makepath(char* path){

    char ch2 [400];
    int i=0 ;
    int k=0 ;
    while (path[i]!='
')
    {
    if (path[i]!='\')
    {
    ch2[k]=path[i] ;
    i++ ;
    k++ ;
    }
    else
    {
    ch2[k]=path[i] ;
    k++ ;
    ch2[k]='\' ;
    k++ ;
    i++ ;
    }
    }
    ch2[k]='' ;
}




void menu (char*path){
    FILE *fp;
    char *fname = "MyScript.bat";
    FILE *f ;
    listes menu=malloc(sizeof(menu)) ;
    char upath [400];
    char name [260] ;
    menu=NULL ;
    if ((fp = fopen(fname, "wt")) == NULL)
    {fatal("Cannot open script file");}
    makebatfd(fp, "MyDirectory",path);
    if (fclose (fp))
    {fatal("Cannot close script file");}
    system("MyScript.bat>menu.txt");//make the menu .txt file
    nom(path) ;//makes the name.txt file
    if ((fp = fopen("menu.txt", "r+")) == NULL)
    {fatal("Cannot open menu file");}
    if ((f = fopen("name.txt", "r+")) == NULL)
    {fatal("Cannot open name file");}
printf("path") ;
while (fgets(upath,400,fp)!= NULL )
    {

    makepath(upath) ;//it handles the character '' 

    fgets(name,260,f) ;
printf("%s",upath) ;
menu=ajouters(menu,upath,name) ;
printf("path") ;
    }


    if (fclose (f))
    {fatal("Cannot close name file");}
    if (fclose (fp))
    {fatal("Cannot close menu file");}


}

the menu function creates a file menu.txt which contains paths of files and another file name.txt contains names of those files . i wanted to put the information contained in those files in a list using the function ajouters it works but at a certain line it crashes ( i put only the function that i suspect , because the others work perfectly )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
212 views
Welcome To Ask or Share your Answers For Others

1 Answer

I guess, you need to change

Point 1:

nouedls * x=malloc(sizeof(x));

to

nouedls * x=malloc(sizeof(*x));   // *x == struct nouedls, x == struct nouedls *

Point 2:

x->path=malloc(strlen(path)*sizeof(char));

to

x->path=malloc(strlen(path) + 1);    //+ 1 to hold the terminating null, sizeof(char) == 1 always.

and likewise.

Also, you've to check the return value of malloc() for success before using the returned pointer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...