0

I downloaded a software package online. Assume the package is called aPackage.zip. After unzip the folder, it contains a executable file Cassie.exe and it takes 2 input files, file1.txt and file2.txt. I just need to double click the Cassie.exe and it automatically start running fine. Now I would like to measure how much time it takes to run Cassie.exe so I wrote a small c++ program (main.cpp) of a new project(TimeMeasure) in Visual C++ 2010 express. However, although I put Cassie.exe, file1.txt and file2.txt inside the same folder, Cassie.exe still keeps complaining it can not open file2.txt. Here are the codes of main.cpp of project TimeMeasure.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>      /* printf */
#include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>       /* sqrt */

using namespace std;

int main () {

  const clock_t begin_time = clock();
  system("C:\\aPackage\\Cassie.exe C:\\aPackage\\file1.txt C:\\aPackage\\file2.txt");

  ofstream myfile;
  myfile.open ("Time.txt");
  myfile << "Time used is %d sec \n"<<float( clock () - begin_time ) /  CLOCKS_PER_SEC;
  myfile.close();
  system("PAUSE");
  return 0;
}

The TimeMeasure project is created at path

C:\Users\Cassie\Documents\Visual Studio 2010\Projects\TimeMeasure

This is why I use the absolute path for the aPackage folder. My pc is window 7 home OS system. Can anyone tell me what I did wrong ? Thank you very much,

Cassie
  • 1,149
  • 5
  • 18
  • 29
  • Not an answer to your _specific_ problem, but http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line – paxdiablo Mar 06 '13 at 08:05
  • 1
    If you `cd` into your project directory then type `C:\aPackage\Cassie c:\aPackage\file1.txt c:\aPackage\file2.txt` - what happens? If it doesn't work, then it might be because cassie does something to the arguments before trying to open them, such as prefixing them with the current working directory. It may be easiest to `chdir()` to `c:\aPackage` first. If you really want to confirm what's happening, you could get a program showing you the `open()` system call that cassie attempts - on UNIX it would be `strace`, on Windows probably one of the sysinternals utilities. – Tony Delroy Mar 06 '13 at 08:09
  • I don't use Windows, but children could inherit the environment of the caller, not set their own. – corazza Mar 06 '13 at 08:09
  • May be the exe needs some delimiters between the file names when run from command line. – Rakesh K Mar 06 '13 at 08:11
  • Thank you all for your comments. I tried Tony' idea and it worked. I have to change my current working path by typing _chdir() function first in main.cpp and then just simply type "system ("Cassie.exe file1.txt file2.txt")". Thank you all again and hope this help others too. – Cassie Mar 06 '13 at 16:59

1 Answers1

0

Cassie.exe probably looks for the two files in the current directory. When you double-click an executable file to run it, the current directory is set to the same directory that the executable file is in, so it works. When you use system() the current directory is unchanged (in this case the directory containing TimeMeasure) so it doesn't work.

Use _chdir to set the current directory before calling system, or try something like

system("cd /d C:\\aPackage && Cassie.exe");

which should have almost exactly the same effect as double-clicking it.

Harry Johnston
  • 33,445
  • 6
  • 56
  • 142