1

I couldn't seem to figure out how to run a cmd program in a C program.

This is my code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char educode[100];
    printf("Welcome To ACE-IT Edu Software!\n");
    printf("\nPlease Type An Educator Code and then press enter.");
    printf("\nEducator Code: ");
    gets(educode);
    if(educode == 5678){
        system("mkdir test");
    } else {
    printf("\nSorry, thats not a valid Educator Code. To buy an Educator Code, go to https://www.ace-it.edu");
    }


    return 0;
}
Shajal Ahamed
  • 141
  • 2
  • 16
John O'Meara
  • 31
  • 1
  • 1
  • 7
  • 1
    Never ever use `gets`, this is a deprecated dangerous function that doesn't check the size of the destination. Use `fgets` instead. – Pablo Apr 08 '18 at 03:42
  • In addition, don't use printf for literal strings. It's pointless. – Roflcopter4 Apr 08 '18 at 07:49

4 Answers4

5

Thanks to the bad if-comparison (you can't compare strings with integers) your system call is never run.

Wrong:

gets(educode);
if(educode == 5678){

Try:

gets(educode);
if(strcmp(educode, "5678") == 0 ){

Remember to add #include <string.h> to the top as well.

Also, never use gets() -- it was removed from the C standard in 2011.

Try fgets(), after reading up on how to use it.

JohnH
  • 2,713
  • 12
  • 21
  • 2
    I didn't feel like rehashing the always-use-`fgets()` answers with the info about fixing line termination. But yes, `gets()` is a security risk as a user can type an arbitrary number of characters, overflowing the buffer (`educode` in this case) - and therefore using `gets()` is always wrong. – JohnH Apr 08 '18 at 03:54
2

The problem with this code is you are comparing a pointer to a string and an integer, in this line.

if (educode == 5678)

5678 is an int type, and you are determining whether or not it equals a pointer to a string of chars. C is an explicitly-typed language, so comparisons like these don't work like that. You will want to use this instead.

if (atoi(educode) == 5678)
    system("mkdir test");

Use the stdlib function atoi() to convert your string to an integer value.

Side note: Using the system() function is the same way to run shell commands across all platforms (Windows, Linux, Mac). However, not all of these commands are the same. For instance, what del does in DOS-based environments, is rm in Linux/Unix. On Windows, you would use rename or move for the same action that mv does on Linux. This program is simple enough, you might just want to use a Batch file, if you're confident this code is only for Windows.

Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
0

I believe you asking for win platform

you could use the system() function available in process.h to run commands.

//Program to run dos commands through a C program.

#include <stdio.h>
#include <process.h>

int main()
{
    int choice=0;

    printf("\n***************************************\n");
    printf("1. Open Notepad...\n");
    printf("2. Get Ip Address...\n");
    printf("3. Shut down the computer...\n");

    printf("** Enter your choice :");
    scanf("%d",&choice);

    switch(choice)
    {
        case 1:
            system("notepad");
            break;
        case 2:
            system("ipconfig");
            system("pause");
            break;
        case 3:
            system("SHUTDOWN -S");
            system("pause");
            break;
        default:
            printf("\n Invalid choice !!!");
    }

    return 0;
}
Roushan
  • 4,074
  • 3
  • 21
  • 38
0

Try the solution in this link:

Calling 'ls' with execv

Make these changes:

args[0] = "/bin/mkdir" 
args[1] = "new_directory"
Free Url
  • 1,836
  • 2
  • 15
  • 28