LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help on Linux commands for a C++ code (https://www.linuxquestions.org/questions/linux-newbie-8/help-on-linux-commands-for-a-c-code-614029/)

cyberfox007 01-16-2008 12:07 PM

Help on Linux commands for a C++ code
 
Hey guys, noob at linux here

I have some c++ code that i wanna run through linux

My program accepts two command line arguments: an iteration count and a “sleep” time.

this is my code

Code:

//test.cpp

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;

void wait (int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main(int argc, char* argv[])
{
 time_t MainTime;
 struct tm* InfoTime;
 int SleepNum, Times;
 int InputSpace[2];
 int i;

 for(i = 1; i < argc; i++)
  InputSpace[i-1] = atoi(argv[i]);
 
 time(&MainTime);
 InfoTime = localtime(&MainTime);
 printf("Program started on %s\n", asctime(InfoTime));

 for(i = 1; i<=InputSpace[0]; i++)
 {
  cout << "\nRun Number " << i;
  cout << "\nWill Sleep for " << InputSpace[1]  <<" seconds\n";
  wait(InputSpace[i-1]);
 }

 time(&MainTime);
 InfoTime = localtime(&MainTime);
 printf("\nProgram ended on %s\n", asctime(InfoTime));
}

i gotta put in my command line something like: "text.exe 7 2(enter)"

my question is how do i run this program in the command line while putting these numbers at the end? (if i need to change any part of my code please let me know). do i need to go into the shell? how do i run it in the shell?

another question is how do i take what i see in the shell and copy it to a text file?

with that done i need to Find out the process ID of test.cpp, Find out all running processes that are related to the name test, While test is still running in one shell issue a command on the other shell that terminates it. and thats it.

Thanx!

jakefolger 01-16-2008 03:11 PM

I don't quite understand your question but I'll give it a shot.

Your first step is to copy your program to your linux box. If you haven't done that yet I suggest emailing test.cpp as an attachment to yourself, moving to the linux box, opening a browser and downloading it to the desktop.

Once you have the file on your desktop open a shell.
type in "cd Desktop" and hit enter.
then type in "ls" and hit enter.

It should list the files on your desktop, and test.cpp should be there.

Next we need to compile your program.
To do that type "g++ test.cpp" and hit enter.

Hopefully it compiles. If not you have a problem with your code.

Once it compiles you'll want to run it.
To do this type in "./a.out [arg1] [arg2]" and hit enter (where [arg1] and [arg2] are your arguments you're sending to the program.

Hopefully that works as you intended. If everything works well up to this point you will want to direct the output to a file.
To do this type "./a.out [arg1] [arg2] > filename"

That should take all output that appeared on the screen the last time you ran the program and send it to a file named filename.

If you get this far let me know and we'll go over the last bit.


All times are GMT -5. The time now is 10:35 PM.