Need the missing line of code

This is a discussion on Need the missing line of code within the C Programming forums, part of the General Programming Boards category; Hey. Using Borland C++ Builder 3 on Win XP Problem: Not quite sure how to test if the item in ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    24

    Need the missing line of code

    Hey. Using Borland C++ Builder 3 on Win XP

    Problem:
    Not quite sure how to test if the item in the direcory im travelling through is a folder. I have had a great big look here, there and everywhere, but I can't seem to apply the code to mine.
    I have maked the line where I wish to insert it.

    Also is there anyway to convert "ent->d_name" into a string?
    You may have noticed i just saved them to a file instead.

    Thanks in advance.

    Code:
    #include <dir.h>
    #include <dos.h>
    #include <conio.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_DIR_PATH 2048
    #define FILE_LEN 200
    
    
    void loadFileList(FILE *, char *, char *);
    void validateFiles(FILE *, char *, char *);
    void getTagDetails(char *, char *, char *, char *);
    void moveFile(char *, char *, char *, char *);
    
    int main(int argc, char *argv[])
    {
        FILE *filePtr = NULL;
        char *dirName;
        char *fileListName = "file list";
        char fileNameFromFile[FILE_LEN] = "";
        char backupFileName[FILE_LEN] = "";
        char artist[FILE_LEN] = "", album[FILE_LEN] = "", title[FILE_LEN] = "";
    
        dirName = argv[1];
    
        loadFileList(filePtr, dirName, fileListName);
    
        //validateFiles(filePtr, fileListName, fileNameFromFile);
    
        filePtr = fopen(fileListName, "r");
    
        if(! filePtr)
        {
            printf("File Not found");
            getch();
            exit(1);
        }
    
        while(fgets(fileNameFromFile, FILE_LEN, filePtr) != NULL)
        {
            strcpy(backupFileName, fileNameFromFile);
            getTagDetails(fileNameFromFile, artist, album, title);
            moveFile(backupFileName, dirName, artist, album);
        }
    
        fclose(filePtr);
    
        printf("\n\ndone");
        getch();
    
        return 0;
    }
    
    
    void loadFileList(FILE *filePtr, char *dirName, char *fileListName)
    {
        DIR *dir;
        struct dirent *ent;
        int fileCount = 0;
    
        filePtr = fopen(fileListName, "w");
    
        dir = opendir(dirName);
    
        if(! dir)
        {
            printf("Directory Not found");
            getch();
            exit(1);
        }
    
        while((ent = readdir(dir)) != NULL)
        {
            if(strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..") && strcmp(ent->d_name, fileListName))
            {
                //want to add line here<<<<<------------------------------------
                {
                    fprintf(filePtr, "%s\n", ent->d_name);
                    fileCount++;
                }
            }
        }
    
        closedir(dir);
        fclose(filePtr);
    
        printf("Number of files = %d\n\n", fileCount);
    }
    
    
    void validateFiles(FILE *filePtr, char *fileListName, char *fileNameFromFile)
    {
        char *ch;
        char delim = '';
        int delimCount = 0;
        int delimError = 0;
        int headingDisplayed = 0;
    
        filePtr = fopen(fileListName, "r");
    
        if(! filePtr)
        {
            printf("File Not Found");
            getch();
        }
    
        while(fgets(fileNameFromFile, FILE_LEN, filePtr) != NULL)
        {
            ch = fileNameFromFile;
    
            while(*ch != '\n')
            {
                if(*ch == delim)
                    delimCount++;
    
                ch++;
            }
    
            if(delimCount != 2)
            {
                delimError = 1;
    
                if(headingDisplayed == 0)
                {
                    clrscr();
                    printf("Syntax of the following files does not match: \n\n");
                    headingDisplayed = 1;
                }
    
                puts(fileNameFromFile);
            }
    
            delimCount = 0;
        }
    
        fclose(filePtr);
    
        if(delimError == 1)
        {
            getch();
            exit(1);
        }
    }
    
    
    void getTagDetails(char *fileNameFromFile, char *artist, char *album, char *title)
    {
        char delim[2] = "";
        char *chPtr;
    
        chPtr = strtok(fileNameFromFile, delim);
        strcpy(artist, chPtr);
        artist[strlen(chPtr)] = '\0';
    
        chPtr = strtok(NULL, delim);
        strncpy(album, chPtr, strlen(chPtr));
        album[strlen(chPtr)] = '\0';
    
        chPtr = strtok(NULL, delim);
        strncpy(title, chPtr, strlen(chPtr) - 1);
    }
    
    
    void moveFile(char *fileNameFromFile, char *dirName, char *artist, char *album)
    {
        struct stat dir_stat;
        char sourceFile[FILE_LEN] = "";
        char destFile[FILE_LEN] = "";
        static int fileCount = 1;
    
        clrscr();
        printf("Moving file %d", fileCount++);
    
        fileNameFromFile[strlen(fileNameFromFile) - 1] = '\0';
    
        strcat(sourceFile, dirName);
        strcat(sourceFile, "\\");
        strcat(sourceFile, fileNameFromFile);
    
        strcat(destFile, dirName);
        strcat(destFile, "\\");
        strcat(destFile, artist);
    
        stat(destFile, &dir_stat);
    
        if(! S_ISDIR(dir_stat.st_mode))
            mkdir(destFile);
    
        strcat(destFile, "\\");
        strcat(destFile, album);
    
        stat(destFile, &dir_stat);
    
        if(! S_ISDIR(dir_stat.st_mode))
            mkdir(destFile);
    
        strcat(destFile, "\\");
        strcat(destFile, fileNameFromFile);
    
        rename(sourceFile, destFile);
        Sleep(500);
    }
    Last edited by Salem; 06-19-2005 at 05:33 AM. Reason: Changed pukey php tags into proper code tags

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also is there anyway to convert "ent->d_name" into a string?
    Code:
    if(strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..") && strcmp(ent->d_name, fileListName))
    It apparently already IS a string, since you're using string functions on it.

    Also, don't just vomit up a bunch of code and expect us to debug the whole thing for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    24
    I can't assign a char * pointer to it, which is what i would like to do.

    As for:
    "Also, don't just vomit up a bunch of code and expect us to debug the whole thing for you."

    I never asked you to debug it, it has no warning's or errors when i run it. I would just like the line as missing code.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    access() works with DJGPP. I don't know if it works with Borland, though, especially version 3 . . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 09:44 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

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