Showing posts with label DOS command. Show all posts
Showing posts with label DOS command. Show all posts

Sunday, September 4, 2011

Dos Command in C

This example will show how to use the DOS command in C and rediret its output to a file..
#include <stdio.h>
#include<conio.h>
void main ()
{
  FILE *fp;
  clrscr();
  printf ("Checking if processor is available...");
  if (system(NULL))
  {
  puts ("Ok");
  }
  else
  {
  printf("Processor is not available");
  getch();
  exit (1);
  }
  system ("dir>c:\dirlist.txt");
  printf("Done!!File created");
getch();
}
output:will create the file with name dirlist in the "c:\" drive..........

Tuesday, August 30, 2011

Running a DOS command using C

To Run a DOS command using C we use the system() function present in process.h and stdlib.h
The prototype of the function is:
int system(const char *command);
On Success returns 0
On Error returns -1
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
printf("This program will execute the DOS command");
i=system("dir");
if(i==0)
printf("completed successfully");
else
printf("error in executing the command");
getch();
}
If you are using turboc3 and run this using Ctrl+F9 you will not see the error , to run this go to cmd prompt and then execute the exe file created after compiling the program form the DOS prompt.you will easily get the output..if you are having any problem...just mail me at milankmr2011@gmail.com