Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simulating DOS commands in C++
New Posts  All Forums:Forum Nav:

Simulating DOS commands in C++

post #1 of 12
Thread Starter 
Hello guys, I am trying to make a program that simulates DIR and MKDIR DOS commands using C++. It's for a project I must do and they told me not to use . I don't have much experience with C++ so I need some help.

This is the code I wrote so far:
Code:
#include <conio.h>
#include <dir.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
struct ffblk fileblock;
int result;

if(argc<2)
{
printf("\nUsage: name.exe [command type] [command's parameters]\n\n");
printf("[command type]\n\t [/D] for DIR command, [/M] for MKDIR command.\n\n");\
printf("[command's parameters]\n\t standard DOS DIR/MKDIR parameters [/P], [/Q] etc.\n\n");

return(1);
}

if(strcmp(argv[1],"/D")==0 || strcmp(argv[1],"/d")==0)
{
result=findfirst("*.*", &fileblock, 0);

if(strcmp(argv[2],"/B")==0 || strcmp(argv[2],"/b")==0)
{
while(result==0)
{
printf("%s\n",fileblock.ff_name);
result=findnext(&fileblock);
}
}

while(result==0)
{
printf("%d\t%d\t%d\t%s\n",fileblock.ff_fdate,fileblock.ff_ftime,fileblock.ff_fsize,fileblock.ff_name);
result=findnext(&fileblock);
}
}
else if(strcmp(argv[1],"/M")==0 || strcmp(argv[1],"/m")==0)
{
result=mkdir(argv[2]);
if(result==0)
{
printf("Directory %s was created.\n",argv[2]);
}
else
{
printf("Unable to create directory");
}
}
else
{
printf("\nWrong command type. [/D] for DIR, [/M] for MKDIR.\n");
}
}

The problems I have are:
  • Don't know how to read directories for the dir command
  • How to check for multiple arguments that DIR command has.
  • How to decode ff_fdate and ff_ftime
  • I guess many more to come frown.gif
    
CPUMotherboardGraphicsRAM
Xeon X5650@4GHz Asus P6T Deluxe Asus GTX760@1254/3305MHz 12GB HyperX@1600MHz CL9 
Hard DriveHard DriveOptical DriveCooling
Seagate 7200.12 1TB (Storage) ExcelStor J8160S 160GB (OS/Programs/Games) Optiarc AD-7203S Scythe Mugen Rev. B 
OSMonitorPowerCase
Windows 7 64bit LG IPS234V 23" 1080p XFX PRO 550W Tt Soprano 
Audio
ADI2000B 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Xeon X5650@4GHz Asus P6T Deluxe Asus GTX760@1254/3305MHz 12GB HyperX@1600MHz CL9 
Hard DriveHard DriveOptical DriveCooling
Seagate 7200.12 1TB (Storage) ExcelStor J8160S 160GB (OS/Programs/Games) Optiarc AD-7203S Scythe Mugen Rev. B 
OSMonitorPowerCase
Windows 7 64bit LG IPS234V 23" 1080p XFX PRO 550W Tt Soprano 
Audio
ADI2000B 
  hide details  
Reply
post #2 of 12
To use DOS commands in C++:

system("INSERT DOS COMMAND HERE");

for example:
Code:
system("cls"); //Clears the console window

if you are using a directory in your dos command remember to use the \\ escape sequence.

so the if you wanted to do cd C:\Windows\System32\ in your program, you would write
Code:
system("cd C:\\Windows\\System32\\");

you dont need to include anything for this (at least not in C++)

EDIT: by the way that code looks like C not C++
Edited by melterx12 - 5/18/11 at 5:54pm
post #3 of 12
Quote:
Originally Posted by melterx12;13555504 
To use DOS commands in C++:

system("INSERT DOS COMMAND HERE");

for example:
Code:
system("cls"); //Clears the console window

if you are using a directory in your dos command remember to use the \\ escape sequence.

so the if you wanted to do cd C:\Windows\System32\ in your program, you would write
Code:
system("cd C:\\Windows\\System32\\");

you dont need to include anything for this (at least not in C++)

EDIT: by the way that code looks like C not C++

It's C++, and he can't use system.h.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #4 of 12
I didn't see a single bit of C++ in that code either. smile.gif

And I thought the idea was to do a simple reimplementation of the DOS commands.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #5 of 12
I didn't see a single bit of C++ in that code either. smile.gif

And I thought the idea was to do a simple reimplementation of the DOS commands.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #6 of 12
Thread Starter 
Well the original project says we should use C++ but we only do lessons on C that's why you see C code because we only learned C console applications. If you know any other way of simulating DIR/MKDIR commands without system.h that is not to advanced for someone with basic C knowledge then please tell me how.
    
CPUMotherboardGraphicsRAM
Xeon X5650@4GHz Asus P6T Deluxe Asus GTX760@1254/3305MHz 12GB HyperX@1600MHz CL9 
Hard DriveHard DriveOptical DriveCooling
Seagate 7200.12 1TB (Storage) ExcelStor J8160S 160GB (OS/Programs/Games) Optiarc AD-7203S Scythe Mugen Rev. B 
OSMonitorPowerCase
Windows 7 64bit LG IPS234V 23" 1080p XFX PRO 550W Tt Soprano 
Audio
ADI2000B 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Xeon X5650@4GHz Asus P6T Deluxe Asus GTX760@1254/3305MHz 12GB HyperX@1600MHz CL9 
Hard DriveHard DriveOptical DriveCooling
Seagate 7200.12 1TB (Storage) ExcelStor J8160S 160GB (OS/Programs/Games) Optiarc AD-7203S Scythe Mugen Rev. B 
OSMonitorPowerCase
Windows 7 64bit LG IPS234V 23" 1080p XFX PRO 550W Tt Soprano 
Audio
ADI2000B 
  hide details  
Reply
post #7 of 12
Quote:
Originally Posted by jNSK;13558376 
It's C++, and he can't use system.h.

system("'); works for me without system.h
post #8 of 12
Quote:
Originally Posted by KaMeNoS;13561656 
Well the original project says we should use C++ but we only do lessons on C that's why you see C code because we only learned C console applications. If you know any other way of simulating DIR/MKDIR commands without system.h that is not to advanced for someone with basic C knowledge then please tell me how.

So, they told you to use C++ for this project, but only taught you C? That's odd.
post #9 of 12
Parsing command line options:
http://www.gnu.org/s/hello/manual/libc/Getopt.html

Creating dirs - (man 3) mkdir

dir command - (man 3) glob, stat
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #10 of 12
I haven't tested this (I don't have a compiler on this PC I'm using) but it should work. This is the basic code for accepting command line arguments and displaying the names of the contents of a directory. If you want to get closer to the actual "dir" command, it's just a matter of accepting even more arguments and formatting the output (read up on dirent.h to figure out how to get the timestamps and so on).

Go to a command line and type "dir /?" to get a list of all the different parameters it accepts. Note that it also checks the DIRCMD environment variable for a user's default settings.
Code:
#include <cstdio>
#include <dirent.h>


void listdir (const char *path)
{
    // Initialize directory pointer and open directory
    DIR *dirptr = NULL;
    dirptr = opendir (path);
    struct dirent *pent = NULL;

    // If not initialized correctly (bad path), return an error
    if (dirptr == NULL)
    {
        printf ("\nError!");
        return;
    }

    // Display directory contents
    while (pent = readdir (dirptr))
    {
        printf ("%s\n", pent->d_name);
    }

    // Close directory
    closedir (dirptr);
}


int main (int argc, char *argv[])
{
    switch (argc)
    {
        case 1: // If there are no arguments, then use current directory
            listdir(".");
            return;
        case 2: // If there is an argument, assume it is a path
            listdir(argv[1]);
            return;
        default:
            printf ("\nCorrect usage is \"mydir <path>\"");
    }
    return 0;
}
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
Server
(11 items)
 
  
CPUMotherboardRAMHard Drive
Intel Core i5-3470 ASRock Z77 Extreme6 16 GB G.Skill Sniper DDR3-1600 1TB WD Caviar Black 
Hard DriveCoolingOSCase
4TB WD Caviar Red Cooler Master Hyper 212+ VMWare ESXi NZXT Crafted Series Tempest 410 
OtherOtherOther
LSI 9280-16i4e RAID Card Intel I350 Quad Port Gigabit NIC Intel Pro/1000 PT Dual Port Gigabit NIC 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simulating DOS commands in C++