Problem with a code

This is a discussion on Problem with a code within the C Programming forums, part of the General Programming Boards category; Hello. I am new to this forum and hoping to find an answer to my problem. My lecturer at college ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Problem with a code

    Hello. I am new to this forum and hoping to find an answer to my problem. My lecturer at college is not quite doing what he is supposed to, and doesn't want to explain to us what certain commands do. My first question is What does strcmp() and strtok() do, and what's the right way to use them.

    Then he asked us to write a C code to simulate the Command Prompt console that we can get in windows. Started off with an easy (i thought) one. Here is the code that I have, but it is not doing what I want it to do. It should be working like the "dir" command in cmd:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <dirent.h>
    
    
    void cmdcom(char);
    int main()
    {
        char* cmdline;
        printf("C:\\ ");
        gets(cmdline);
    
    
        char* cmd;
        char* para1;
        char* para2;
    
    
        cmd=strtok(cmdline,"");
        para1=strtok();
        para2=strtok();
    
    
    
    
    
    
        if(strcmp(cmd,"CD")==0)
        {
            //CD command
        }
        if(strcmp(cmd,"dir")==0)
        {
            cmdcom(cmd);
        }
        return 0;
    }
    void cmdcom(char dirname)
    {
        DIR *dir;
        struct dirent *ent;
    
    
            dir = opendir(dirname);
    
    
                if(dir!=NULL)
            {
            while ((ent=readdir(dir))!=NULL)
            {
                printf("%s\n",ent->d_name);
            }
            closedir(dir);
        }
        else
        {
            perror("");
        }
    }
    I am problably using strtok wrong, so any feedback is really appreciated. Also hints as to how to tackle a CD command, would be useful.

    Regards,
    Artur.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > char* cmdline;
    > gets(cmdline);
    1. Stop using gets(), read this -> SourceForge.net: Gets - cpwiki
    2. You didn't allocate any space to store your input.
    Try something like
    char cmdline[BUFSIZ];
    fgets( cmdline, BUFSIZ, stdin );


    > I am problably using strtok wrong, so any feedback is really appreciated. Also hints as to how to tackle a CD command, would be useful.
    Yes, it takes 2 parameters, not zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    The idea for using gets() was kind of forced byt the lecturer, but I've no problem with changing that. I made the changes that you mentioned, but still, don't quite get how to make this function to actually work for me. I know that the cmdcom function works fine, because when I put it in main, it does the job but I just don't know how to probably pass the parameter for the function to read which directory it has to open.

    About strtok, again, this part of the code was given to us by the lecturer, with no explanation at all, so it is quite confusing for me.

    Thank you for a fast reply.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    My lecturer at college is not quite doing what he is supposed to, and doesn't want to explain to us what certain commands do. My first question is What does strcmp() and strtok() do, and what's the right way to use them.
    No excuse. The whole of the Internet is out there and searchable.

    http://www.google.com/q=man+strcmp

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Artur Leonowicz View Post
    I am problably using strtok wrong, so any feedback is really appreciated. Also hints as to how to tackle a CD command, would be useful.
    Look at the example from the FAQ and play around with it. If you have further questions, ask here.

    About the "cd" command:
    "cd" takes a directory as its parameter. Thus if you get for example the string "cd programs" from the user, you have to tokenize it (split it up into its part). You would get "cd" and "programs" as a result. Now you can call your function for changing directories with the second string (the directory name) as its argument.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by AndiPersti View Post
    Look at the example from the FAQ and play around with it. If you have further questions, ask here.

    About the "cd" command:
    "cd" takes a directory as its parameter. Thus if you get for example the string "cd programs" from the user, you have to tokenize it (split it up into its part). You would get "cd" and "programs" as a result. Now you can call your function for changing directories with the second string (the directory name) as its argument.

    Bye, Andreas
    Thank you for the reply. I got as far as this with my understanding of strtok. I still don't get how to pass the parameter to the function after tokenizing it. I had a look at the FAQ, but it doesn't really answer my question. What is the correct way of passing tokenized parameter to a function?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    For strtok, I've just written a simple program in the another post - Hopefully it helps:
    Storing a string inside an array?

    As for strcmp - This is easy! Put two strings in it and it returns 0 if they are the same!

    I find the best way of finding info on functions is to type this into Google

    msdn c language function_name()

    -> Choose the (CRT) option.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Artur Leonowicz View Post
    What is the correct way of passing tokenized parameter to a function?
    strtok() returns a pointer to char, so your function should accept such a type
    Code:
    void cmdcom(char dirname)
    Your version only takes a char as its parameter. You have to change the parameter to a pointer to char.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 10:50 AM
  2. Problem with this code
    By miclus in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2004, 08:15 PM
  3. problem with my code
    By stilllearning in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 03:02 PM
  4. Problem with code
    By brian0918 in forum C++ Programming
    Replies: 6
    Last Post: 08-12-2002, 09:14 AM
  5. Code Problem!!
    By Ruski in forum C++ Programming
    Replies: 21
    Last Post: 07-02-2002, 05:36 AM

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