简体   繁体   English

当我使用bash运行程序时,C fopen无法正常工作

[英]C fopen isn't working, when I run program with bash

I have simple bash script 我有简单的bash脚本

#!/bin/bash

make
./code1_test/test_syn1
./code2_test/test_syn2
./code3_test/test_syn3

test_syn are my program tests. test_syn是我的程序测试。 In every of these programs, I'm using fopen command to open .txt file. 在所有这些程序中,我都使用fopen命令打开.txt文件。 If I run programs from command line, I have a correct test output, but when I'm using this bash script, fopen return me NULL, so I can't open my .txt files and I can't run tests. 如果我从命令行运行程序,我将获得正确的测试输出,但是当我使用此bash脚本时,fopen将返回NULL,因此我无法打开.txt文件,也无法运行测试。 Why is this happening, and how can I fix this? 为什么会这样,我该如何解决?

If you're using fopen() with a relative path, you should execute the script from its directory. 如果将fopen()与相对路径一起使用,则应从其目录执行脚本。 Try this: 尝试这个:

#!/bin/bash
make
(cd code1_test/ && exec ./test_syn1)
(cd code2_test/ && exec ./test_syn2)
(cd code3_test/ && exec ./test_syn3)

Otherwise your program tries to open your .txt in the directory from where your run your Bash script, that's not what you want. 否则,程序将尝试在运行Bash脚本的目录中打开.txt ,这不是您想要的。

If you want to be able to run your program from anywhere, you might try not to use a relative path when you fopen() . 如果您希望能够从任何地方运行程序,则可以在fopen()时尝试不使用相对路径。

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

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