Computing.Net > Forums > Programming > how to use Dos commands in Borlad C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

how to use Dos commands in Borlad C

Reply to Message Icon

Name: moonnightingale
Date: October 30, 2002 at 03:46:54 Pacific
OS: Win Xp
CPU/Ram: 1.1GHz/128
Comment:

I want to make a small program in C language to whom when i run should Ping specific range of IP addresses on my LAN system.For example my range is from 192.168.0.41 to 192.168.0.59 so every
time if i want to check the number of users online i need to pickup and dial at lab or secondly if phone is busy i use to Ping each IP address and it is very hectic.So i want to make a program to whom when i run should ping whole series of 18 IP and it will tell that number of users alive
and dead.Secondly it will be more better if it also give me alive IP addresses.So please help me.Donot tell me to use IP Scanner i know many of them but i want to design my own small and simple.i donot need fency software i just need information about users. Please tell me how we can
use Dos commands of Ping ,netstat,ipconfig in Borland C.



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff Graver
Date: October 30, 2002 at 08:26:14 Pacific
Reply:

Standard C libraries provide a function called system() that allows you to run shell commands. So you could say something like system("ping 192.168.0.41"); to ping one of the addresses.

I write this from memory so the syntax may be off.

Jeff


0

Response Number 2
Name: Jim
Date: October 30, 2002 at 08:39:23 Pacific
Reply:

Actually, I think the API for running a program is CreateProcess(). However, there is a little bit of work involved. If you want to use system() it's easier, if not quite as flexible.


0

Response Number 3
Name: mm_freak
Date: October 30, 2002 at 09:01:37 Pacific
Reply:

It's easy. For all systems you can use system(), exec*() (there are quite a few of them), spawn*() (also a few). For Win16 (Win 3.xx or earlier) you can use WinExec() and for Win32 you _should_ use CreateProcess(). Here are the differences:

system() is the simplest function to execute a shell command. You just pass a string, like you would enter it on the command line and system() executes it. If you want to have the output of, let's say, ping, you just change the stdout for that programm:
system("ping 192.168.0.1 > output.txt");

exec*() and spawn*(): I don't know the exact syntax of them, since I've never used any one of them. They are much more flexible than exec(), you can, for example pass an array of command line arguments to programs you want to spawn.

WinExec() and LoadModule() are like exec*() and spawn*(), but they allow a greater control over the newly spawned program. Unlike the above functions, they're available under Windows only.

CreateProcess() finally is a hell of a lot more flexible than all other functions. It allows you the exactly control the spawned program. You can debug it, control standard input/output/error, check out, when the new process terminates and much more. This function is available under Win32 (Win95,98,ME,NT,2000,XP) only.

You must know, what's best for you, in the simplest case you would call system() and redirect the output to a file:
system("ping xxx > file.txt");
Then you would open that file.txt, it will contain the exact output of the ping command.


0

Response Number 4
Name: moonnightingale
Date: October 30, 2002 at 10:54:58 Pacific
Reply:

i am using Borland C++ version 3.1 In help of C++ i did not find any details about system() please guide me how to use it and what header file it requires


0

Response Number 5
Name: mm_freak
Date: October 31, 2002 at 19:52:33 Pacific
Reply:

The system() call is as simple as the command line itself, here is the syntax:

int system(char *cmd);

cmd is a null-terminated string containing a command line command to be executed. You can pass anything, you would enter on the command line. The return value is the return value of your command processor (command.com in DOS, cmd.exe in WinNT and your shell program (bash, for example) in Linux). If the command fails, the return value is -1 and errno is set appropriately.

And as i said, you can redirect the output of called programs by adding a greater-than sign (>).

Example 1: Call netstat and exit

int main() {
system("netstat -ano");
return 0;
}

Example 2: Ping local host and save results

int main() {
FILE *fl;

system("ping 127.0.0.1 > temp.txt");
fl = fopen("temp.txt","rt");
if (fl) {
/* Process contents of temp.txt */
fclose(fl);
}
return 0;
}

Remember, system() does not return, untel the command was executed.


0

Related Posts

See More



Response Number 6
Name: moonnightingale
Date: November 1, 2002 at 06:03:46 Pacific
Reply:

I have installed Borland C in WinXP i tried ur programm in C but it did not work and gave following errors.It was compiled with out error but while running it ave error.
Does it matter that i run it in XP mode or i switchover to pure DOS mode in order to run this programme.
The file name is ping which i made.
*****************
Linking PING.EXE:
Linker Warning: No module definition file specified: using defaults
Linker Error: Undefined symbol _system in module PING.CPP
******************
thanks for ur kind help


0

Sponsored Link
Ads by Google
Reply to Message Icon

Disabling Ctrl+Alt+Del in... C++ Error Spawning cl.exe



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: how to use Dos commands in Borlad C

How to use make command www.computing.net/answers/programming/how-to-use-make-command/3765.html

How to use string array in C++? www.computing.net/answers/programming/how-to-use-string-array-in-c/9478.html

DOS commands in C www.computing.net/answers/programming/dos-commands-in-c/9449.html