Thread: help

  1. #16
    Registered User
    Join Date
    May 2002
    Posts
    14
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    struct treenode {
    char filename[256];
    struct treenode* leftchild;
    struct treenode* rightchild;
    };

    typedef struct treenode TreeNode;

    /*
    This function returns the index of the last occurence of '.' in
    the string str. It returns -1 if str is NULL or str has no '.'
    */
    int lastIndexDot(char* str) {
    //insert code here, so recognzie the "."

    int len;
    len=strlen(str);

    if( str == NULL)
    return 0;

    while(--len!=-1){

    if( str =='.' &&
    return len+1;
    }
    return len;
    }

    /*
    This function compares the relative ordering of file1 and file2.
    The definition of ordering is given in the TMA paper.
    It returns -1 if file1 < file2, returns 0 if file1 = file2,
    and returns if file1 > file2
    */
    int compareFilenames(char* file1, char* file2) {
    //insert code here for string compare
    if(strcmp(file1,file2)==NULL){
    return 0;

    else if(strcmp(file1,file2)> 0)
    return +1;

    else if(strcmp(file1,file2)<0)
    return -1;
    }

    return 0;
    }

    /*
    The function inserts the newfile string into the binary tree pointed
    by root. The funtcion returns the root pointer of the binary tree
    with the newfile insert. The function does nothing but return NULL
    if newfile already exists in the binary tree
    */
    TreeNode* insertFilename(TreeNode* root, char* newfile) {
    //insert code here

    if(root==NULL){
    root=(TreeNode*)malloc(sizeof(TreeNode));
    root-> strcpy(FileName,newfile);
    root-> leftchild =NULL;
    root-> rightchild=NULL;
    }
    else if( compareFilenames(file1, file2)<0){
    root-> rightchild=insertFilename(root->rightchild,newfile);

    else if( compareFilenames(file1, file2)<0 )
    root->leftchild=insertFilename(root-> leftchild,newfile);

    return (root);

    }


    /*
    This function prints the content of the binary tree in the inorder
    traversals with counters.
    */
    int printInorderWithCounter(TreeNode* root, int count) {

    return 0;
    }

    /*
    This function prints the content of the binary tree in the inorder
    traversals
    */
    void printInorder(TreeNode* root) {
    if (root == NULL)
    return;
    printInorder(root->leftchild);
    printf("%s\n", root->filename);
    printInorder(root->rightchild);
    }

    int numberOfFiles(TreeNode* root) {
    if (root == NULL)
    return 0;
    return numberOfFiles(root->leftchild) +
    numberOfFiles(root->rightchild) + 1;
    }

    int main()
    {
    FILE* fp; /* file pointer for popen */
    char buffer[256]; /* buffer for holding filename temporary */
    TreeNode* root = NULL; /* the root pointer to the binary tree */
    /*
    declare other necessary variables
    */

    fp = _popen("dir /a-d /b", "rt");
    if (fp == NULL) {
    printf("Error in listing directory\n");
    }

    /*
    make changes to the following code

    */

    root= insertFilename( root, newfile);

    while (fgets(buffer, 256, fp) != NULL) {
    buffer[strlen(buffer)-1] = '\0'; /* removing the ending newline */
    printf("%s\n", buffer);


    }
    }
    Thanks hammer

    I just have problems with comparefilename function and Lastdotindex. The insertionnode function should work. It is using comparefilename to compare file1 string and file 2 string.

    Notice I put insert code on lastindexdo and comparefilename.

    Underneath, i write some code myself. I am not sure it is right.
    Last edited by The gib; 05-28-2002 at 04:37 AM.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Before I guide you any further, can I just clarify the flow you want your program to have.

    - insertFilename() will call:
    - compareFilenames() which will then call twice:
    - lastIndexDot()

    compareFilenames will need to call lastIndexDot twice, once for each string, and store the result in two different ints.

    These two ints will then be used to position char*'s onto the filename strings, in the position of the suffixes.

    Then strcmp can be used to determine the difference between the two extensions, and return the result to insertFilename.
    Is this the way you want it? It is not the way I'd recommend as it is over engineered, but it's your program, and you've got to do it the way you are comfortable with.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    14
    Originally posted by Hammer
    Before I guide you any further, can I just clarify the flow you want your program to have.


    Is this the way you want it? It is not the way I'd recommend as it is over engineered, but it's your program, and you've got to do it the way you are comfortable with.
    So, what should I do.

    I need lastindexdot() to recognize every string. Should i eliminate one function.

    Insertion() is not overengineer. It is a a recursion.

    I only have problem with lastindexdot() and comparefunction.

    Comparefunction, I only call once.

    As for insertion(), I guess it is ok.

  4. #19
    Registered User
    Join Date
    May 2002
    Posts
    14
    Hammer,
    What do you suggest I should do in comparefunction and lastindexdot()?

  5. #20
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >So, what should I do.
    >I need lastindexdot() to recognize every string. Should i eliminate one function.
    Yes, eliminate one. At this point, you don't need a seperate function to find the last dot, that's what strchr() is for.

    >Insertion() is not overengineer. It is a a recursion.
    I only meant the part of having 2 seperate functions (compare and lastindex), not the recursion part.

    >As for insertion(), I guess it is ok.
    I haven't looked at it.

    If you don't have much reference material to hand, here's a link for you. It's a paper on binary trees, with example code. It may help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #21
    Registered User
    Join Date
    May 2002
    Posts
    14
    Is there a reference related to recognize the suffix?and sort them out?

    The binary tree part is part is already done.

    I just need compare() and lastindex().

  7. #22
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Wink

    Originally posted by Hammer
    At this point, you don't need a seperate function to find the last dot, that's what strchr() is for.
    And for the first dot you use strrchr?

  8. #23
    Registered User
    Join Date
    May 2002
    Posts
    14
    Originally posted by Monster

    And for the first dot you use strrchr?
    So, what should I write in code?

    I already try write some code,but fail

  9. #24
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think you should write the same code as Hammer wrote
    in his second(?) post. Forget about the lastindex function
    and only write the compare function (using the strrchr function).

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    opps, typo alert! I meant
    strrchr()

    not strchr()

    The difference is that one finds the first occurence, the other finds the last occurence.

    You should use strrchr().

    >Is there a reference related to recognize the suffix?and sort them out?
    Not that I know of, it's all down to string manipulation. There are a few functions that allow you to interogate a string, strrchr() being one.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #26
    Unregistered
    Guest
    hammer,
    your program

    if ((p1 = strrchr(file1, '.')) == NULL)

    so, I find the suffix

    len= strlen(file1);

    after I find the length.

    length - p1

    will this work.
    Urgenet!
    help.

  12. #27
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, say your filename is myfile.txt, which is stored in char *file1.

    This means

    char *file1 points to the first character of myfile.txt.

    After doing this:
    >if ((p1 = strrchr(file1, '.')) == NULL)

    you have a new pointer, p1, which is pointing to:
    >.txt
    which is the backend of the file1 string.

    If you want the length of the complete filename (including extension) you do:

    len = strlen(file1);

    If you want the extensions length you do:
    len = strlen(p)-1; (-1 decrements the count to allow for the dot).

    If you want the length of the filename only (not including extension)

    len = p - file1 - 1;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #28
    Registered User
    Join Date
    May 2002
    Posts
    14
    */
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    struct treenode {
    char filename[256];
    struct treenode* leftchild;
    struct treenode* rightchild;
    };

    typedef struct treenode TreeNode;

    /*
    This function returns the index of the last occurence of '.' in
    the string str. It returns -1 if str is NULL or str has no '.'
    */
    int lastIndexDot(char * str) {


    char *pointer; //string

    pointer = strrchr( str, '.');//count the position of the '.'

    if(pointer)
    return pointer - str; //subtracts '.'

    else
    return 0; //returns 0, if no extension

    }

    /*
    This function compares the relative ordering of file1 and file2.
    The definition of ordering is given in the TMA paper.
    It returns -1 if file1 < file2, returns 0 if file1 = file2,
    and returns if file1 > file2
    */
    int compareFilenames(char* file1, char* file2) {

    //I think this is the problem
    //the comparison is excluding the suffix

    if( strcmp(file1+lastIndexDot(file1),
    file2+lastIndexDot(file2) )< 0 )
    return -1;//returns -1,if file1 is lesser than file2

    if( strcmp(file1+lastIndexDot(file1),
    file2+lastIndexDot(file2) )> 0 )
    return +1; //returns 1, if file1 is greater than file2

    if( strcmp(file1+lastIndexDot(file1),//return 0,if file1 equals file2
    file2+lastIndexDot(file2) )== 0 )
    return 0;

    if( strcmp(file1+lastIndexDot(file1),
    file2+lastIndexDot(file2) )==NULL )
    return 0; //returns 0,if file has extension is null


    if(strcmp(file1,file2)== 0)
    return -1;


    }

    /*
    The function inserts the newfile string into the binary tree pointed
    by root. The funtcion returns the root pointer of the binary tree
    with the newfile insert. The function does nothing but return NULL
    if newfile already exists in the binary tree
    */
    TreeNode* insertFilename(TreeNode* root, char* newfile) {


    if(root==NULL){
    root=(TreeNode*)malloc(sizeof(TreeNode));
    strcpy(root-> filename, newfile);
    root-> leftchild =NULL;
    root-> rightchild=NULL;
    }
    else if( compareFilenames( root->filename , newfile )<0){
    root-> rightchild=insertFilename(root->rightchild,newfile);
    }
    else if( compareFilenames(root->filename , newfile )>0 ){
    root->leftchild=insertFilename(root-> leftchild,newfile);
    }

    return (root);

    }


    /*
    This function prints the content of the binary tree in the inorder
    traversals with counters.
    */
    int printInorderWithCounter(TreeNode* root, int count) {

    return 0;
    }

    /*
    This function prints the content of the binary tree in the inorder
    traversals
    */
    void printInorder(TreeNode* root) {
    if (root == NULL)
    return;
    printInorder(root->leftchild);
    printf("%s\n", root->filename);
    printInorder(root->rightchild);
    }

    int numberOfFiles(TreeNode* root) {
    if (root == NULL)
    return 0;
    return numberOfFiles(root->leftchild) +
    numberOfFiles(root->rightchild) + 1;
    }

    int main()
    {
    FILE* fp; /* file pointer for popen */
    char buffer[256]; /* buffer for holding filename temporary */
    TreeNode* root = NULL; /* the root pointer to the binary tree */
    /*
    declare other necessary variables
    */

    fp = _popen("dir /a-d /b", "rt");
    if (fp == NULL) {
    printf("Error in listing directory\n");
    }


    while (fgets(buffer, 256, fp) != NULL) {

    buffer[strlen(buffer)-1] = '\0'; /* removing the ending newline */
    root= insertFilename( root, buffer); //insertfilename

    }
    printInorder(root);

    fflush(stdin);
    getchar();

    }


    This is the new code. It works. It is not perfect yet, because the same suffix is not on the screen. ONly different suffix works. I want the same suffix on the screen,not to screen out any files.
    For example,

    file.doc
    name.doc
    love.exe

    this is actual programming code

    file.doc
    love.exe

    It doesn't include name.doc suffix. In order words, it excludes name.doc

    Please help! I don't know what should I do. I think I have a little arithmetic error.

    I already write down the main problem is in comparefile().
    Please check out this out.


    Thanks hammer,
    your method is good, but complex. Well. At least, it is complicated for me.
    Last edited by The gib; 05-29-2002 at 06:25 AM.

  14. #29
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hi gib, glad to see you're almost there.....

    Your problem is in the insertFilename() function. You are checking the return codes from compareFilenames() to determine if they are greater than zero, or less than zero. However, you are not checking to see if it is exactly zero. I suggest making one of the tests do both, EG

    Code:
    else if (compareFilenames(root->filename, newfile) >= 0)
    Whilst I'm here:

    >fflush(stdin);
    .. is bad. See here.

    >return statement missing from main().
    Add return (0); at the end of main().

    And please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #30
    Registered User
    Join Date
    May 2002
    Posts
    14
    Hammer,
    You are the best.

Popular pages Recent additions subscribe to a feed