os independent file separator - Unix

This is a discussion on os independent file separator - Unix ; hi am coding in c (using gcc of cygwin) .and am parsing a directory by giving at cmdline a.exe F:\code\c\testparse ..the function must return a char** pointing to an array of jpeg filenames in the dir.. (ie,an elment of array ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: os independent file separator

  1. os independent file separator

    hi
    am coding in c (using gcc of cygwin) .and am parsing a directory by
    giving at cmdline a.exe F:\code\c\testparse ..the function must return
    a char** pointing to an array of jpeg filenames in the dir..
    (ie,an elment of array will be "F:\sajan\code\c\testparse\me.jpeg")
    I have learned how to use scandir with filter fn .my problem is that
    if some other fn wants to process these filenames sothat the imagedata
    of jpeg files can be retrieved etc then the filenames should be "F:\
    \sajan\\code\\c\\testparse\\me.jpeg" .how can i do this modification
    inside the parse() efficiently..
    strcat'ing \\ is windows specific isn't it? is there some way to make
    it os independent?(like ,java has a separatorChar..)
    my current version looks like this .

    char** parsedirectory(char * dirname){
    printf("parsedirectory()\n");
    struct dirent **filelist = NULL;
    char * directory =dirname;
    int fcount = -1;
    int i = 0;
    fcount = scandir(directory, &filelist, selectjpeg, alphasort);
    if(fcount < 0) {
    perror(directory);
    return NULL;
    }

    char* fldr=malloc(strlen(dirname)+2);
    if (fldr==NULL){
    printf("malloc failed at fldr");
    exit(EXIT_FAILURE);
    }
    strcpy(fldr,dirname);
    strcat(fldr,"\\");
    printf("fldr:%d:%s\n",strlen(fldr),fldr);

    char** imgnamelist=malloc(fcount*sizeof(*imgnamelist));
    if (imgnamelist==NULL){
    printf("malloc failed at imgnamelist");
    exit(EXIT_FAILURE);
    }
    for(i=0;i char* filenmtmp=malloc(strlen(fldr)+1+strlen((filelist[i])->d_name));
    if (filenmtmp==NULL){
    printf("malloc failed at filenmtmp");
    exit(EXIT_FAILURE);
    }

    strcpy(filenmtmp,fldr);
    strcat(filenmtmp,(filelist[i])->d_name);
    printf("filenmtmp:%s\n",filenmtmp);

    imgnamelist[i]=malloc( (strlen(filenmtmp)+1)
    *sizeof(**imgnamelist));
    if (imgnamelist[i]==NULL){
    printf("malloc failed at imgnamelist[%d]",i);
    exit(EXIT_FAILURE);
    }

    strcpy(imgnamelist[i],filenmtmp );

    free(filenmtmp);
    free(filelist[i]);

    }

    free(filelist);
    free(fldr);
    return imgnamelist;
    }



  2. Re: os independent file separator

    In article
    <8ce3a8a2-3452-4213-becf-39c41ba38d23@i7g2000prf.googlegroups.com>,
    oswald.harry@gmail.com wrote:

    > hi
    > am coding in c (using gcc of cygwin) .and am parsing a directory by
    > giving at cmdline a.exe F:\code\c\testparse ..the function must return
    > a char** pointing to an array of jpeg filenames in the dir..
    > (ie,an elment of array will be "F:\sajan\code\c\testparse\me.jpeg")
    > I have learned how to use scandir with filter fn .my problem is that
    > if some other fn wants to process these filenames sothat the imagedata
    > of jpeg files can be retrieved etc then the filenames should be "F:\
    > \sajan\\code\\c\\testparse\\me.jpeg" .how can i do this modification
    > inside the parse() efficiently..
    > strcat'ing \\ is windows specific isn't it? is there some way to make
    > it os independent?(like ,java has a separatorChar..)
    > my current version looks like this .


    Don't Windows and Cygwin allow you to use Unix-style pathname syntax,
    e.g. F:/sajan/code/c/testparse/me.jpeg?

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  3. Re: os independent file separator



    >
    > Don't Windows and Cygwin allow you to use Unix-style pathname syntax,
    > e.g. F:/sajan/code/c/testparse/me.jpeg?
    >
    >


    didn't know that..
    thanx for the help
    oharry

+ Reply to Thread