从 windows CMD 创建一个 "file.dat"

Create a "file.dat" from the windows CMD

当运行使用以下 C++ 脚本时,我获得了在 Gnuplot 中工作所需的数据。

#include "stdio.h"

double TT;/* Imaginary part of T matrix */
double x;/* Real part of Energy */
double y;/* Imaginary part of Energy */
double G;/*Resonance Peak width */
double ER;/* Resonance Peak Energy */

main()
{
G = 120.;
ER=180.;
for(x=150.;x<=200.;x=x+1.){
for(y=-70.;y<=-50.;y=y+0.5){

/* Plotting T as a function of x and y */
TT = (G/2.)*(y+(G/2.))/(ER*ER - 2.*ER*x + G*y + (G*G/4.) + x*x + y*y);
printf("%g %2.1f %f\n", x,y,TT); /*Printing out T to standard output*/
}
printf("\n"); /* printing a newline after every column(this part is crutial) */
}
}

从命令提示符 I 运行 以下命令行:

.D_Data start notepad++ > mytextfile.dat

在Gnuplot中调用点DAT文件时 我收到以下警报:

gnuplot> splot 'mytextfile.dat'
         warning: Bad data on line 1 of file mytextfile.dat
         Not enough input columns

如果文件内容被复制并插入到记事本++中的新文件中并以扩展名Dat保存;那么它 运行 在 Gnuplot 中是正确的

我已经尝试使用这些命令行但没有任何结果

// C:\projects\helloworld> .D_Data ./a.out > 3Ddata
// PS C:\projects\helloworld> .D_Data > result.txt
// PS C:\projects\helloworld> .D_Data | Out-File -FilePath .\OUTPUT.txt
// PS C:\projects\helloworld> .D_Data test.exe > myoutput.txt
// PS C:\projects\helloworld> .D_Data copy > SN.txt
// PS C:\projects\helloworld> .D_Data echo > filename2.txt
// C:\projects\helloworld> .D_Data /S > Myfile.dat

我注意到的一点是,从 Notepad++ 创建的文件的大小几乎是原来的两倍。

是否可以从 Gnuplot 函数接受的 CMD 中获取文件?

G N U P L O T
         Version 5.4 patchlevel 1 last modified 2020-12-01

好吧,您使用的代码看起来更像 C 而不是 C++。 (这也很好)

在 C 中,您可以使用 fprinf 代替 printf .. 看这里:

#include <stdio.h>

int main () {
   FILE *file;
   const double G = 120.;
   const double ER=180.;

   file = fopen("file.dat","w+");//create or overwrites a file
   
   for(int x=150; x<=200; x++){
      for(double y=-70.; y<=-50.; y += 0.5){
         TT = (G/2.)*(y+(G/2.))/(ER*ER - 2.*ER*x + G*y + (G*G/4.) + x*x + y*y);
         fprintf(file,"%g %2.1f %f\n", x, y, TT); //writes into a file
      }
      fprintf(file,"\n");//same here
   }

   fclose(file);//flushes the file and closes it
   return(0);
}

在 C++ 中,您可以使用 fstream 代替 printf .. 看这里:

#include <fstream>
#include <format>

int main () {
   
   std::ofstream file("file.dat", std::ofstream::out | std::ofstream::trunc);

   const double G =  120.;
   const double ER = 180.;
   
   for(int x=150; x<=200; x++){
      for(double y=-70.; y<=-50.; y += 0.5){
         TT = (G/2.)*(y+(G/2.))/(ER*ER - 2.*ER*x + G*y + (G*G/4.) + x*x + y*y);
         file << x << " " << std::format("{:2.1f}",y) << TT << "\n"; //writes into a file // EDIT: I added the C++20 std::format
      }
      file << std::endl;//same here
   }

   file.close();//flushes the file and closes it
   return(0);
}

一些建议:

  • 修复所有警告(你应该有一些)
  • 尽量做到适当的空格和缩进!
  • 使变量名可读
  • 尝试制作人类可以阅读的源代码(如果可能,不加注释)
  • 尝试创建表达您的计算的函数

编辑:我将 C++ 示例中的命令更改为 std::format(..),因为代码中有错误。

TT(x,y) = (G / 2.) * (y + (G / 2.)) / (ER * ER - 2. * ER * x + G * y + (G * G / 4.) + x * x + y * y)
ER=180.
G = 120.
set table 'test3D.dat'
splot [150:200] [-70:-50.1] TT(x,y)
unset table
splot 'test3D.dat' with lines