简体   繁体   English

无法读取bash行生成的文件

[英]Can't read file generated by bash line

I have written a C code where the bash script lines are used inside this C code, and this is how I wrote it: 我编写了一个C代码,其中在该C代码中使用了bash脚本行,这就是我的写法:

printf("wc -l < smallerthan > number_lines\n");   
if( (fr=fopen(fname_rcut,"r"))==NULL )  {  printf("error in rcut file:   %s\n",fname_rcut);  exit(1);  }  

I need to read the file "number_lines" which is generated from "smallerthan" file, the problem is when I source the C code to run automatically like: 我需要读取从“ smallerthan”文件生成的文件“ number_lines”,问题是当我将C代码作为源自动运行时:

$gcc myC_code.c -lm     
$./a.out > run.sh  
$source run.sh   

Then if I view the run.sh 然后,如果我查看run.sh

& vi run.sh 

I get this inside run.sh : 我在run.sh得到了这个:

    wc -l < smallerthan > number_lines  
    ls 

    error in rcut file: /home/number_lines

which mean the code upto this point didn't find my "number_lines" file yet since the number_lines file is yet to appear, but if I copy the line and run it separately, instead of "automatically", then it works because the file is there now. 这意味着到目前为止的代码尚未找到我的“ number_lines”文件,因为尚未出现number_lines文件,但是如果我复制该行并单独运行而不是“自动”运行它,那么它将起作用,因为该文件是就是现在。

My question is, how to make my code run automatically and my C code to read the file which is generated by bash line or how to generate the file and read it properly? 我的问题是,如何使我的代码自动运行以及我的C代码读取bash行生成的文件,或者如何生成文件并正确读取它?

Any idea please because I'm really new to programming and I have to use bash inside C for my work. 任何想法都可以,因为我真的是编程新手,所以我必须在C中使用bash进行工作。

Note: the above is only small part of my C code but I used several bash lines inside my C code. 注意:以上只是我的C代码的一小部分,但我在C代码中使用了几行bash行。

There are a number of observations in your code. 您的代码中有许多观察结果。 I assume that char *fname_rcut indeed points to "/home/number_lines" . 我认为char *fname_rcut确实指向"/home/number_lines"

First observation: if you write commands to a file, they will not be executed.So the file number_lines is created only after you run run.sh . 首先要注意的是:如果将命令写入文件,则不会执行命令,因此仅在运行run.sh之后才创建文件number_lines Therefore, the file will not exist during the execution of your C program. 因此,该文件在C程序执行期间将不存在。 You might look into int system(const char *command) ( man 3 system ). 您可能会研究int system(const char *command)man 3 system )。

Second observation: /home/number_lines is probably not the correct filename. 第二观察: /home/number_lines可能不是正确的文件名。 It would probably be /home/your_name/number_lines ; 它可能是/home/your_name/number_lines ; try a pwd to see what the exact directory name is. 尝试输入pwd以查看确切的目录名称是什么。

Third observation: Why do you want to source run.sh ? 第三次观察:为什么要获取source run.sh Source executes the file in the current shell. Source在当前shell中执行文件。 There is usually no need for that. 通常不需要这样做。

I have solved it : 我已经解决了:
what we need actually is using system(command) after each shell command 我们实际需要的是在每个shell命令之后使用system(command)
for example : 例如 :

printf("wc -l < smallerthan > number_lines\n");     

will be after solving : 将解决后:

sprintf(command1,"wc -l < smallerthan > number_lines\n");  
system(command1); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM