GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2008, 07:57
NatsoumiMaya NatsoumiMaya is offline
New Member
 
Join Date: Feb 2008
Posts: 2
NatsoumiMaya is on a distinguished road

Double linked List & File System


i have a code to write in C where i have to implement the mkdir, rm, rmdir and touch functions using double linked lists...im haveing trouble with it...Bellow is the function discription and the code i have so far:
createDir: creates an entryNode for the new directory, copying the argument string into dynamically allocated space for the directory name, then adds the new file to the working directory's entries list.

touchFile:creates an entryNode for the new text file, copying the argument string into dynamically allocated space for the file name. It then reads the contents of the simulated text file from standard input, and stores it into suitably allocated dynamic storage. (You should not assume any bound on the amount of text.) Two consecutive carriage returns
(that is, '\n' followed immediately by another '\n') indicates the end of
this text. The first is stored in the contents string, followed by a null
character; the second carriage return is not stored. The new file is
then added to the working directory's entries list.


removeFile/removeDir: removes the named file/directory from the working directory's entries list, and frees all dynamically allocated storage associated with the file.

here is the code i have so far i have so far:
HEADER FILE:
CPP / C++ / C Code:
#ifndef _DIRECTORY_H
#define _DIRECTORY_H

[color="Green"]/* tree node data structure declaration */[/color]
struct entryNode {
char * name;
struct entryNode * next; [color="green"]/* sibling */[/color]
struct entryNode * previous; [color="green"]/* sibling */[/color]
int isDirectory;
struct entryNode * parent;
union {
char * contents; [color="green"]/* if this node is a file, this contains the content of the file */[/color]
struct entryNode * entryList; [color="green"]/* if this node is a directory,
this contains a pointer to the first child inside the directory */[/color]} entry;
};
[color="green"]/*THE REST OF THE FILE CONTAINS THE FUNCTION PROTOTYPES*/[/color]
.C FILE

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "directory.h"

#define TRUE 1
#define FALSE 0

#define MAX_TEXT_SIZE 8192

[color="Green"]/* used as a temp storage for the text content of a file */[/color]
char tmpbuf[MAX_TEXT_SIZE];

[color="green"]/* pointer to the root node of the file system */[/color]
struct entryNode * root;

[color="green"]/* Helper functions */[/color]
void pwdHelper (struct entryNode *); [color="green"]/* don't worry about this one */[/color]


[color="green"]/* Return a pointer to the entry with the given name in the given list,
or NULL if no such entry exists. */
/* basically it searches for the file/dir name at the current level:
* it does not go into directories */[/color]
struct entryNode * located (char * name, struct entryNode * list) {
     if (list == NULL) {
          return NULL;} 
     else if (strcmp (list->name, name) == 0) {
          return list;} 
     else {
          return located (name, list->next);}
}

[color="green"]/* implements the "touch" command (one argument; not in standard UNIX) */
/* wd is a pointer to the current working directory */[/color]void touchFile (struct entryNode * wd, char * fileName)
{
     struct entryNode * newFile; 
     if (located (fileName, wd->entry.entryList)){
           printf ("touch: %s: File exists\n", fileName);
           return;}

[color="green"]/* initialize temporary text buffer to zeros */[/color]
bzero(tmpbuf,MAX_TEXT_SIZE);

[color="Red"][b]/* HELP NEEDED HERE*/[/b][/color]
}

/[color="Green"]* implements the "mkdir" command (one argument; no options) */[/color]
void createDir (struct entryNode * wd, char * dirName) {
     struct entryNode * newDir;
     if (located (dirName, wd->entry.entryList)){
          printf ("mkdir: %s: File exists\n", dirName);
          return;}
     [b][color="Red"]struct entryNode * newDir = (char *) malloc(sizeof(newDir));
     newDir->entry.entryList;
     newDir->next = NULL;
     newDir->previous = dirName;
     dirName->next = newDir;
     struct entryNode * entryList = NULL;
/*HELP NEEDED WITH BOLDED TEXT*/[/color][/b]}

[color="Green"]/* implements the "rm" command (one argument, unlike standard UNIX; no options) */[/color]
void removeFile (struct entryNode * wd, char * fileName) {
     struct entryNode * file;
     file = located (fileName, wd->entry.entryList);
     if (file == NULL){
          printf ("rm: %s: No such file or directory.\n", fileName);
          return;}
     else if (file->isDirectory){
          printf ("rm: %s: is a directory.\n", fileName);
          return;}
[b][color="Red"]/*HELP NEEDED HERE*/[/color][/b]
}

[color="Green"]/* implements the "rmdir" command (one argument, unlike standard UNIX; no options) */[/color]
void removeDir (struct entryNode * wd, char * dirName) {
     struct entryNode * dir;
     struct entryNode * tmp;
     dir = located (dirName, wd->entry.entryList);
     if (dir == NULL){
         printf ("rmdir: %s: No such file or directory.\n", dirName);
         return;}
     else if (!dir->isDirectory){
         printf ("rmdir: %s: Not a directory.\n", dirName);
         return;}
     else if (dir->name[0] =='/'){
         printf("rmdir: Cannot delete root directory.\n");
         return;}
[color="Red"][b]     else{
         previousfile = file->previous;
         nextfile = file->next;
         if (previousfile==NULL)
         {
               printf("%s: No Previous File");
               if (nextfile == NULL)
               {
                     printf("%s: No Next File");
               }
               free(file);
               wd->entry.entrylit = NULL;
         }
         else if
         {
               if (nextfile == NULL)
               {
                    printf("%s: No Next File");
                    if (previousfile==NULL)
                   {
                      printf("%s: No Previous File");
                    }
                    free(file);
                    wd->entry.entrylit = NULL;
               }
         }
         else
         {
                 free(file);
                 wd->entry.entrylit = NULL;
          }
} 

/* HELP NEEDED WITH BOLDED TEXT */[/b][/color]}

[color="Green"]/* implements the "mv" command (two arguments, unlike standard UNIX; no options) */[/color]
void moveEntry (struct entryNode * wd, char * from, char * to) {
    struct entryNode * fromNode;
    struct entryNode * toNode;
    fromNode = located (from, wd->entry.entryList);
    if (fromNode == NULL)
    {
        printf ("mv: %s: No such file or directory.\n", from);
        return;
     }

[color="Red"][b]/*HELP NEEDED HERE */[/b][/color]
}
THANK YOU IN ADVANCE FOR YOUR HELP ^_^
Last edited by admin : 09-Feb-2008 at 08:08. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 10-Feb-2008, 08:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Double linked List & File System


Quote:
Originally Posted by NatsoumiMaya
i have a code to write in C
Here's my request:

1. When you post the code, don't put in markups for colors and bold stuff, etc. Put the code between code tags: Put [c] before the first line of C code and put [/c] after the last line of C code. Just type your question(s) and other information into the post with no markups at all

2. Since this is obviously not a "first assignment," the class obviously has some programming language prerequisite(s). You are obviously expected to know how to compile and debug something. Tell us what you tried; don't just post the assignment and some defective code and issue a general request for help.

Ask specific questions about what it is that you don't understand. If you tried to compile and the compiler gave errors or other messages that you don't understand, then post the exact message(s). Don't paraphrase; paste the entire message into your post. If there were gobs and gobs of messages, just post the first ten or so. (If the compiler reports an error in, say, line78, then show us what line 78 is. Maybe add a comment in the file , but don't put some uncompileable stuff in the file at that point. Maybe we want to try compiling it before responding to you.)

OK, I'll try to unravel some of your post and show you what I mean.

Here is what I think your createDir function looks like:
CPP / C++ / C Code:
void createDir(struct entryNode *wd, char *dirName)
{
    struct entryNode *newDir;
    if (located(dirName, wd->entry.entryList)) {
        printf("mkdir: %s: File exists\n", dirName);
        return;
    }
    struct entryNode *newDir = (char *) malloc(sizeof(newDir));
    newDir->entry.entryList;
    newDir->next = NULL;
    newDir->previous = dirName;
    dirName->next = newDir;
    struct entryNode *entryList = NULL;
}

When you compiled the program did your compiler complain about any part of this function? What were the messages? What do you not understand about why this code is defective?

Since different compilers have different ways of reporting errors and issuing warnings, it is sometimes useful to those who would like to help you if you tell us what compiler and what operating system you are using for your development.

Debugging code is just as much a learning process as writing code. Some would say it's even more of a learning process.

Instead of just attempting to "fix" some particular piece of code, we might be able to walk you through parts of the debugging process if we have more information about your environment.


Regards,

Dave
Last edited by davekw7x : 10-Feb-2008 at 09:36.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
Double output leanieleanz C++ Forum 1 11-Mar-2005 20:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:32.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.