2013-05-27 69 views
0

我有一个粒子系统程序,它在每次迭代中生成一个带粒子坐标的.dat文件。最终目标是通过具有不同参数的脚本多次运行程序。因此,我试图以一种方式设置我的程序,对于每次运行,所有相关数据都将存储在一个文件夹中。不正确地使用系统()调用?

我要做的就是以从.dat文件PNGsGnuplot,叫ffmpeg创建一个视频输出的PNGs的,使用WinRAR压缩.dat文件,最后清理,删除所有的中间文件。这工作,当我在工作目录中。

现在我尝试创建一个新的目录,并在那里做同样的事情。我的代码:

// Load the proper library to use chdir() function 
#ifdef _WIN32 
#include <direct.h> 
#elif defined __linux__ || defined __APPLE__&&__MACH__ 
#include <unistd.h> 
#endif 

// Make output directory and change working directory to new directory 
    ostringstream dirCommand; 
    dirCommand << "mkdir " << folderName_str; 
    system(dirCommand.str().c_str()); 
    const char* test = folderName_str.c_str(); 
    #ifdef _WIN32 
     if(_chdir(test)) 
     { 
      printf("Unable to locate the directory: %s\n",test); 
      return; 
     } 
    #elif defined __linux__ || defined __APPLE__&&__MACH__ 
     if(chdir(test)) 
     { 
      printf("Unable to locate the directory: %s\n",test); 
      return; 
     } 
    #endif 
     else 
      printf("Created output directory...\n"); 

已经是这部分,我知道会有反对意见。我已经广泛地观察了SO,许多人赞成Windows的SetCurrentDirectory(),或者他们对使用system()持怀疑态度。在我的防守,我是新手程序员,我的知识实在有限......

现在,当我试图让视频与FFMpeg,然后RAR /焦油我的文件:

// Make video 
     std::cout << "Generating Video..." << endl; 
     ostringstream command; 
     command << "ffmpeg -f image2 -r 1/0.1 -i output_%01d.png -vcodec mpeg4 " << videoName_str << ".avi -loglevel quiet"; 
     std::system(command.str().c_str()); 

     // Clean Up! 
     std::cout << "Cleaning up!" << endl; 
     ostringstream command2; 
     #ifdef _WIN32 
      command2 << "rar -inul a " << videoName_str << ".rar *.dat settings.gp loadfile.gp"; 
     #elif defined __linux__ || defined __APPLE__&&__MACH__ 
      command2 << "tar cf " << videoName_str << ".tar *.dat settings.gp loadfile.gp"; 
     #endif 
     std::system(command2.str().c_str()); 

我得到Win/Linux中非常不同的行为。

Win 7 x64,Visual Studio 2010/12

在Windows中,该文件夹已创建。 .dat文件生成正确,gnuplot也绘制PNGs。当ffmpeg被调用时,没有任何反应。没有从FFMpeg或任何错误消息。 WinRAR也是如此。也许,最后一件事,我可以使用免费的7z命令行工具!

Linux Mint的14 64,Qt的4.8.1

奇怪的是,该行为是由该Windows的反转。只要第一个.dat文件被更改,就会更改目录。这就好像我为我的文件生成对fprintf()所做的每个后续调用都不起作用,或者在某处丢失了。 Gnuplot作品,如ffmpegtar !!

我真的很困惑。任何帮助将非常感激。

+1

我认为只要编写shell脚本来做这件事会更好。您可能需要为每个平台单独使用一个,但是谁在乎 - 现在获得的条件编译相当于相同的东西。 –

+0

谢谢!我真的沉迷于此。但要带回家的信息是学习如何管理我的错误。我必须更好地预测无效的用户输入。 – Dima1982

回答

1

的几点可能会有所帮助:

  1. 确保检查每个系统调用,包括系统()和fprintf()的结果。

  2. 自从我上次触摸Windows以来已经有一段时间了;我记得根据二进制文件的链接方式,它们不会总是打印到同一个控制台。所以ffmpeg/winrar可能会抛出错误信息,或者只是分配一个新的,短暂的控制台来打印。

  3. 我会使用mkdir/_mkdir而不是调用system()。

  4. 通过popen()/ _ popen(),您可以更好地控制错误输出。

  5. 考虑使用shell脚本或bat文件。

+0

我想越容易越好!我会考虑制作一个脚本。我会记住你的有用提示。我需要学习错误控制! – Dima1982