?
Solved

Net send command prompt in C++

Posted on 2003-02-27
11
Medium Priority
?
355 Views
Last Modified: 2011-09-20
I am using C to do this program that can send message across LAN and networks...and can send a lot..
using the for loop...heehee
BUt how can I do it in C++??
especially using the FILE open stuff,write in the autoexe batch file..
can someone show me how to do it in C++??
thanks

the program:


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


main()
{
     char ip[100];
     char message[250];
     char cmd[5000];
     int times;
     int choice;
     
     FILE *ptr;
     
     int i;


     printf("---------------------\n");
     printf("Net send bomber..");
     printf("\n");
     printf("---------------------\n");
     printf("\n");
     printf("1.Net Send Bomber\n");
     printf("2.Quit\n");
     printf("Your Selection: ");
     scanf("%d", &choice);
     fflush(stdin);
     

     
     if(choice==1){
          printf("Target (PC Name / IP): ");
          gets(ip);
          printf("Message: ");
          gets(message);
          printf("Number of times: ");
          scanf("%d", ×);

          ptr = fopen("C:\\temp.bat", "w");
          if(ptr)
          {
               for(i=0; i<times; i++)
               {
                    fprintf(ptr, "net send %s %s\n", ip, message);
               }
               fclose(ptr);
               sprintf(cmd, "C:\\temp.bat");    
               system(cmd);    
          }
     }
     else return(0);
     
               
     return 0;
}


0
Comment
Question by:joely2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 2

Expert Comment

by:corduroy9
ID: 8033558

Are you asking how to make this console program a Windows program?

Or are you asking for the C++ commands that do the same thing as your code does?  (ie instead of using char*, using CString objects?  or instead of printf() using cout?

You can use the same exact code tha you have, put it in a cpp file, and it will work the same.


0
 

Assisted Solution

by:DavidCrow
DavidCrow earned 80 total points
ID: 8034503
Is this a code snippet that hasn't compiled yet?  I'm seeing that 'i' and 'W' haven't been declared.

You might want to change the scanf() statement to:
   scanf("%d", &W);

While it's not in error, the following two statements could be combined into one:
   sprintf(cmd, "C:\\temp.bat");    
   system(cmd);  
Since there are no variable substitutions being made, you could just use:
   system("C:\\temp.bat");  
0
 

Author Comment

by:joely2k
ID: 8034863
yeah corduroy9...
I want it in C++ although I know it can be compile in cpp...
I want the printf change to cout...
cstring and more....
dear David crow...that "w" is for write command for C ...
write the data into the batch file...

I want you all to tell me how to do exactly this same program using C++ commands,strings,types..

thanks..
your help will be highly apprieciated
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:DavidCrow
ID: 8035074
Lines like

   printf("Net send bomber..");

can be replaced with

   std::cout << "Net send bomber..";

and lines like

   char ip[100];
   gets(ip);

can be replaced with

   std::string ip;
   std::getline(std::cin, ip);

Does that help?
0
 

Author Comment

by:joely2k
ID: 8036033
what is the command for writing the message into our autoexec batch file using C++?

ptr = fopen("C:\\temp.bat", "w");

#include <iostream.h> does not support it..
so how??
#include <stdio.h> and #include <iostraem.h>
together will lead to crash?

help me....
0
 
LVL 2

Accepted Solution

by:
corduroy9 earned 80 total points
ID: 8036133


here's a little sample code, you should be able to find a lot of your answers online, if not in microsoft's MSDN web site, then just type "ios::nocreate" (or any other keyword) into a google search...


     ifstream fin;
     fin.open ( "C:\\testRead.txt", ios::in|ios::nocreate, filebuf::sh_none );
     if ( ! fin.is_open() )
          return false;
     fin.read ( block, 100 );    
     fin.close ( );





     ofstream fout;
     fout.open ( "c:\\testWrite", ios::out, 0 );
     if ( ! fout.is_open() )
          return false;
     fout.write ( "Test", 4 );
     fout.close ();



0
 

Expert Comment

by:DavidCrow
ID: 8036203
#include <fstream.h>
ofstream file;
file.open("c:\\temp.bat");
file.write(...);
file.close();
0
 

Author Comment

by:joely2k
ID: 8037849
thanks alot....this might help me alot man...
I go and try first..
besides that....if got any other solutions or method just put in first ...hehehe
0
 

Author Comment

by:joely2k
ID: 8038865
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream.h>



int main ()
{
     char ip[100];
     char message[250];
     char cmd[5000];
     int times;

     ofstream file;

     //net send test..using command prompt..

     cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
          <<"\n\n\t\t       Net send tester BETA"
          <<"\n\t\t   Type in the IP/Computer Name"
          <<"\n\t\t     Then input the message.."
          <<"\n\t\tand how many times you wanna send!"
          <<"\n\n\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;

     cout<<"\nEnter IP/Computer name => ";
     cin.getline(ip, 99);
     cout<<"Type message           => ";
     cin.getline(message, 249);
     cout<<"Send how many times    => ";
     cin>>times;

     file.open("c:\\temp.bat", ios::out,0);
     for(int i=0;i<times;i++)
     {
          file.write("net send ip message");
     }

     file.close();
     
     system(cmd);









     getch();
     
     return(0);
}


oh gosh I really dunno how to apply the C++ commands inside the file.write(" ");
cos later I have to relate it in the command prompt
for net send...

//fprintf(ptr, "net send %s %s\n", ip, message);
Tat's I know for C....

and what is the command for C++ in sprintf??
for this statement?

//sprintf(cmd, "C:\\temp.bat");  
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 8041129

In MS VC++, you should use CString objects.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.asp

For sprintf, use the CString::Format() method as follows:

CString sText;
sText.Format( "string = %s number = %d", "test", 99 );

And you can put CString objects inside the file.write() command.

Sounds like you should buy/borrow a intro to C++ book.  It will have all kinds of examples for you.


0
 
LVL 11

Expert Comment

by:bcladd
ID: 9847338
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between corduroy9 and DavidCrow

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question