?
Solved

Net send command prompt in C++

Posted on 2003-02-27
11
Medium Priority
?
357 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
  • 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
Technology Partners: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

621 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