feel free to laugh at my code!

This is a discussion on feel free to laugh at my code! within the C Programming forums, part of the General Programming Boards category; ok, i have been working on this unix emulator for a few days. i have to hand it in tomorrow ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    feel free to laugh at my code!

    ok, i have been working on this unix emulator for a few days. i have to hand it in tomorrow (gulp)
    it takes in a unix command from the user such as 'cat' which displays the contents of a file or 'mv' which renames a file, and is supposed to process the instuctions.

    i am new to this and have taken it as far as i can. i have had a lot of help from the people who contribute on this site and i'd like to say a big thank you to you all whoever you are! : )

    alas, the actual program is diabolical! i had to laugh when i saw the amount of errors i got when i tried to run it! (it was either that or cry!)

    anyhow below is the code, please feel free to howl with laughter at my ignorance of the c language. if you would like to point out my major mistakes id really appreciate it (if you find over 100 i'll get the noose ready!)
    now, where's that application form for macdonalds........
    jim.



    i couldn't figure out how to view the contents of a folder...(ls) so i've omitted that.





    #include <stdio.h>
    #include <conio.h>
    #include <dos>.h>
    #include <stdlib.h>
    #include <string.h>

    void CopyFile();
    void GetFileName(char []);



    void main (void)

    {
    char command [7] [4];
    strcpy(command[0],"date");
    strcpy(command[1],"time");
    strcpy(command[2],"ls");
    strcpy(command[3],"cat");
    strcpy(command[4],"rm");
    strcpy(command[5],"cp");
    strcpy(command[6],"mv");

    char selection [255];
    char file[80];
    char oldname[80], newname[80];


    clrscr();

    printf("This is a Unix Operating System Emulator");
    printf("$");




    scanf("%s", &selection);

    if strcmp(selection, command [0]) == true;
    date();

    else if strcmp(selection, command [1]) == true;
    time();

    //else if strcmp(selection, command [2]) == true;
    ls();

    else if strcmp(selection, command [3]) == true;
    cat();

    else if strcmp(selection, command [4]) == true;
    rm();

    else if strcmp(selection, command [5]) == true;
    cp();

    else if strcmp(selection, command [6]) == true;
    mv();

    else {
    printf("Error!");

    }



    time(){
    struct dostime_t t;
    _dos_gettime(&t);
    printf("The current time is: %2d:%02d:%02d.%02d\n", t.hour, t.minute,
    t.second, t.hsecond);
    }


    date(){
    struct date d;

    getdate(&d);
    printf("The current year is: %d\n", d.da_year);
    printf("The current day is: %d\n", d.da_day);
    printf("The current month is: %d\n", d.da_mon);

    }

    cat(){
    FILE *fptr, *fopen();
    ftpr = fopen("filename", "r");
    }

    rm(){


    /* prompt for file name to delete */
    printf("File to delete: ");
    gets(file);

    /* delete the file */
    if (remove(file) == 0)
    printf("Removed %s.\n",file);
    else
    perror("remove");
    }

    cp(){
    {
    CopyFile();

    return 0;
    }

    void GetFileName(char filename[])
    {
    fgets(filename,256,stdin);
    filename[ strlen(filename) -1 ] = '\0';

    }

    void CopyFile(){

    char source[256];
    char dest[256];
    int character;

    printf("Enter source filepath: ");
    GetFileName(source);
    FILE *fsource = fopen(source,"r");
    if(fsource == NULL)
    {
    printf("Source File Does Not Exist!");
    exit(1);
    }

    printf("Enter destination filepath: ");
    GetFileName(dest);
    FILE *fdest = fopen(dest,"w");

    while( fscanf(fsource,"%c", &character) != EOF)
    {
    fprintf(fdest, "%c", character);
    }
    printf("File copied sucessfully");
    }


    mv(){
    char oldname[80], newname[80];

    /* prompt for file to rename and new name */
    printf("File to rename: ");
    gets(oldname);
    printf("New name: ");
    gets(newname);

    /* Rename the file */
    if (rename(oldname, newname) == 0)
    printf("Renamed %s to %s.\n", oldname, newname);
    else
    perror("rename");

    }

    return 0;

    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I don't use linux so my implimentation is different but here are a few tips:

    Code:
    //Change 7 4 to 7 5 so that it can hold the strings properly
    //You have to account for the null zero '\0' added by strcpy 
    char command [7][4];
    char command [7][5];
    Also
    Code:
    #include<stdio.h>
    ...
    
    //declare you functions for example
    void date();
    void cat();
    ...
    
    int main()
    {
    ...
    }
    
    //define the functions
    void date()
    {
    ...
    }

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    This is by no means a complete answer, but I have a few suggestions:

    Code:
    #include <dos>.h>
    ...change this to...
    #include <dos.h>
    Code:
    char command [7] [4]; 
    strcpy(command[0],"date"); 
    strcpy(command[1],"time"); 
    strcpy(command[2],"ls"); 
    strcpy(command[3],"cat"); 
    strcpy(command[4],"rm"); 
    strcpy(command[5],"cp"); 
    strcpy(command[6],"mv");
    
    ...may be replaced with...
    
    char **command =
    {
      "date",
      "time",
      "ls",
      "cat",
      "rm",
      "cp",
      "mv"
    };
    From main():
    Code:
    //else if strcmp(selection, command [2]) == true; 
    ls();
    This will cause you problems because the function ls() will be executed each time. Simply remove both lines or comment out the call to ls(). Also in main(), your large if .. else if block may be replaced by a switch statement.

    Further down I noticed:
    Code:
    cat(){ 
    FILE *fptr, *fopen(); 
    ftpr = fopen("filename", "r"); 
    }
    You should remove *fopen() from the second line, leaving FILE *fptr;. Also, all this function does is open a file called filename. The name of the file cannot be specified by the user, the contents of the file are not read or displayed, and the file is not closed in this function.

    Additionally, the function cp() does nothing but call CopyFile(). Perhaps cp() could be removed and you could call CopyFile() directly from main().

    As I mentioned in the beginning, this is by no means a comprehensive list of the problem within the code you posted. Something you might wish to try is to try and compile the code, then focus on and correct the first error the compiler prints. Rinse. Repeat. This isn't the way you would normally check code, but I think it may serve you well during your cram session tonight ;)
    Jason Deckard

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Your code
    if strcmp(selection, command [0]) == true;
    date();

    this will not compile, you need to put () with your if statement

    if( strcmp(selection,command[0]) == 0 )
    data();

    strcmp returns 0 if the 2 string are equal, not true.
    Change all that and you should get rid of alot of errors

    Here is a reference site i like to use. You can find alot of information about functions there.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    10
    wow! thanks everyone!
    i have taken your advise once again and have got the errors down to 1 yes! just 1!!!
    perhaps you can spot it......

    it is to do with the time() function. im getting a declaration syntax error for it. how can that be???

    im going to college in 2 hours to hand it in. maybe i'll become a c programmer after all! ; )
    jim




    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>
    #include <string.h>



    void CopyFile();
    void GetFileName(char []);


    void main (void)


    void time() /*declaration syntax error here*/
    void date()
    void cat()
    void rm()
    void cp()
    void mv()



    {
    char command [6] [5];
    strcpy(command[0],"date");
    strcpy(command[1],"time");
    strcpy(command[2],"cat");
    strcpy(command[3],"rm");
    strcpy(command[4],"cp");
    strcpy(command[5],"mv");

    char selection [255];
    char file[80];
    char oldname[80], newname[80];


    clrscr();

    printf("This is a Unix Operating System Emulator");
    printf("$");




    scanf("%s", &selection);

    if (strcmp(selection, command [0]) == 0)
    date();

    else if (strcmp(selection, command [1]) == 0)
    time();


    else if (strcmp(selection, command [2]) == 0)
    cat();

    else if (strcmp(selection, command [3]) == 0)
    rm();

    else if (strcmp(selection, command [4]) == 0)
    cp();

    else if (strcmp(selection, command [5]) == 0)
    mv();

    else {
    printf("Error!");

    }







    time(){
    struct dostime_t t;
    _dos_gettime(&t);
    printf("The current time is: %2d:%02d:%02d.%02d\n", t.hour, t.minute,
    t.second, t.hsecond);
    }


    date(){
    struct date d;

    getdate(&d);
    printf("The current year is: %d\n", d.da_year);
    printf("The current day is: %d\n", d.da_day);
    printf("The current month is: %d\n", d.da_mon);

    }

    cat(){
    FILE *fptr;
    ftpr = fopen(" ", "r");
    }

    rm(){


    /* prompt for file name to delete */
    printf("File to delete: ");
    gets(file);

    /* delete the file */
    if (remove(file) == 0)
    printf("Removed %s.\n",file);
    else
    perror("remove");

    cp(){
    {
    CopyFile();

    return 0;
    }

    void GetFileName(char filename[])
    {
    fgets(filename,256,stdin);
    filename[ strlen(filename) -1 ] = '\0';

    }

    void CopyFile(){

    char source[256];
    char dest[256];
    int character;

    printf("Enter source filepath: ");
    GetFileName(source);
    FILE *fsource = fopen(source,"r");
    if(fsource == NULL)
    {
    printf("Source File Does Not Exist!");
    exit(1);
    }

    printf("Enter destination filepath: ");
    GetFileName(dest);
    FILE *fdest = fopen(dest,"w");

    while( fscanf(fsource,"%c", &character) != EOF)
    {
    fprintf(fdest, "%c", character);
    }
    printf("File copied sucessfully");
    }


    mv(){
    char oldname[80], newname[80];

    /* prompt for file to rename and new name */
    printf("File to rename: ");
    gets(oldname);
    printf("New name: ");
    gets(newname);

    /* Rename the file */
    if (rename(oldname, newname) == 0)
    printf("Renamed %s to %s.\n", oldname, newname);
    else
    perror("rename");

    }

    return 0;

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  2. Any good places for free source code?
    By VegasSte in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-19-2002, 04:22 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. to which extent - Free source code
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-09-2002, 01:47 PM
  5. Code Warrier for free - we'll almost (UK)
    By bigtamscot in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-13-2002, 06:49 AM

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