help

This is a discussion on help within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <string.h> #include <stdlib.h> struct treenode { char filename[256]; struct treenode* leftchild; struct treenode* rightchild; }; typedef struct ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    14

    help

    #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) {
    return -1;
    }

    /*
    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) {
    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) {
    }


    /*
    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
    */
    while (fgets(buffer, 256, fp) != NULL) {
    buffer[strlen(buffer)-1] = '\0'; /* removing the ending newline */
    printf("%s\n", buffer);
    }
    }
    I can compare the files using suffix.
    I want to sort it out. Like .doc , .txt in a order

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you please

    - Tell us what your program is supposed to do
    - Tell us what your program is doing wrong, or what you would like it to do.
    - What have you tried to do to fix/enhance it.
    - tells us specifically what help you would like.
    - post with code tags.

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

  3. #3
    Unregistered
    Guest
    Tree Node.

    I want to recognize suffix of the file. For example, there are many files, such as add.txt, man.doc, love.cfg

    I want to organize the suffix

    like

    love.cfg will be first
    man.doc will be second
    add.txt will be third.

    Furthermore, I want to add branches inside my tree. My tree can add new files, such as ego.doc

    in this case,

    after the insertion

    love.cgf
    man.doc
    ego.doc
    add.txt

    This is very tough. I am working on it now for a month, without success.

    I read many books related to it. However, i can't do the suffix thing. I know I have to use string compare. However, it just crash.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, to help you along a bit, here's a function that takes 2 strings containing filenames, and returns the result of a strcmp on the filename extension (suffix). Hope this helps get you moving along!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int compareFilenames(char *, char *);
    
    int main(void)
    {
    	char *f1 = "file1.txt";
    	char *f2 = "file2.doc";
    	
    	printf ("compareFilenames says %d\n", compareFilenames(f1, f2));
    	return 0;
    }
    	
    	
    int compareFilenames(char *file1, char *file2)
    {
    	/* Compare the extensions of the filenames that
    	 * are in the two pointers.
    	 * returns the result of a strcmp() function.
    	 */
    	char *p1, *p2;
    	
    	if ((p1 = strrchr(file1, '.')) == NULL)
    	{
    		printf("%s has no extension\n", file1);
    		return (1);
    	}
    	
    	if ((p2 = strrchr(file2, '.')) == NULL)
    	{
    		printf("%s has no extension\n", file2);
    		return (-1);
    	}
    	
    	return ((strcmp (p1, p2)));
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    How ever, this is not the point.

    The program only returns '.' on position.

    the suffix .doc and .txt, .doc suppose to be ahend of .txt.

    This is the point of the program.

    Thanks anyway.

  6. #6
    Unregistered
    Guest
    By the way, your idea is good.

    When I compare them. I get 16.
    I still couldn't get compare the .doc and .txt.

    I try to use strlen. I have no idea how to cut off til '.' I know all the character are in the string.

    cut.doc
    love.txt

    I still couldn't cut off the first character, then use the suffix for sorting. The sorting part is easy. I just have to do bubble sort.

    The comparison part is hard.

    many thanks.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You do realise my program does the comparison between the 2 file extensions?

    The 16 is difference between them. Look at the line
    >return ((strcmp (p1, p2)));
    At this point p1 and p2 point to the file extensions in each filename.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    14
    Originally posted by Hammer
    You do realise my program does the comparison between the 2 file extensions?

    The 16 is difference between them. Look at the line
    >return ((strcmp (p1, p2)));
    At this point p1 and p2 point to the file extensions in each filename.
    I understand you are using strcmp to compare two string.
    However, i don't understand the return value 16.

    I know you cut off the first value by using strrchr.

    Can you please elaborate on that? You don't need to write codes for me. I just need to understand what to do with the return value 16.

    Tips?
    Last edited by The gib; 05-27-2002 at 08:36 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The return code 16 is directly from strcmp. Look up that function here.

    int strcmp(const char *s1, const char *s2);

    The strcmp() and strncmp() functions return an integer
    less than, equal to, or greater than zero if s1 (or the
    first n bytes thereof) is found, respectively, to be less
    than, to match, or be greater than s2.
    You can use the compare function when inserting new items in your list. You just need to run through each node in the list, comparing the filename within each node to the one you are trying to insert. By using the return code you know if the extension should be inserted before or after the current node.

    Does this help?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    #define size 15

    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) {



    if(str==NULL)
    return -1;


    if( strchar(file1, ".")== )



    return -1;
    }

    /*
    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) {


    if(strcmp(file1,file2)==NULL)
    return 0;

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

    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) {

    int count=0;

    if(root == compareFilenames(char* file1, char* file2)){

    root = (TreeNode *) malloc(sizeof(TreeNode));
    root-> filename[count]= newfile;
    root->leftchild = NULL;
    root->rightchild=NULL;
    }
    else if(NewItem



    }


    /*
    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 */


    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 */
    printf("%s\n", buffer);
    }
    }

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    14
    I have trouble connecting comparefile and lastdotindex suffix

    Help!

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You don't really need your own seperate function to find the last dot, that's what strchr() is for. However, if you do, you would probably do best by returning a char* not an int, unless you need to know the actual number of characters from the start to the dot.

    If you do want lastIndexDot() to return an int, all you need to do is get a pointer to the dot (using strchr) then subtract this pointer from the pointer str. This works because each pointer holds a memory location, subtract one from the other and you get the difference between them.

    I probably haven't explained my version of the code well enough, so I'll do that now:

    >if ((p1 = strrchr(file1, '.')) == NULL)
    this gets a pointer (p1) to the first dot within the string. It does not alter the string itself.

    This is then repeated using p2 and the second string.

    So now when we do the compare, we are actually comparing something like this:
    >strcmp(".txt", ".doc");

    As you have probably read by now, the strcmp function returns an int, based on the difference between the 2 strings it is comparing.

    If this doesn't help let me know and I'll try again!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    #define size 15

    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) {



    if(str==NULL)
    return -1;


    if( strchar(file1, ".")== )



    return -1;
    }

    /*
    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) {


    if(strcmp(file1,file2)==NULL)
    return 0;

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

    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) {

    int count=0;

    if(root == compareFilenames(char* file1, char* file2)){

    root = (TreeNode *) malloc(sizeof(TreeNode));
    root-> filename[count]= newfile;
    root->leftchild = NULL;
    root->rightchild=NULL;
    }
    else if(NewItem



    }


    /*
    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 */


    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 */
    printf("%s\n", buffer);
    }
    }

    I try it on this program. It doesn't work.

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    14
    new version,

    #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) {


    return -1;
    }

    /*
    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) {

    if( file1+lastIndexDot(char* str)




    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) {


    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
    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);


    }
    }

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you use code tags when you post code please..... (see my signature).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21