test_group内容:
./test1
./test1000
./test10000
假设从test_group文件里读取test1,test1000, test10000内容
int main(int argc, char* argv[])
{
FILE* fp = fopen(argv[1], "r");
char testfile[128];
while (fgets(testfile, sizeof testfile, fp) != NULL) {
//去除换行符
if (testfile[strlen(testfile) - 1] == '\n')
testfile[strlen(testfile) - 1] = '\0';
FILE* tf = fopen(testfile, "r");
//.........一系列读取操作
}
}
假设该c程序为main.c, gcc编译后可执行文件为main
Linux上:
./main ./test_group
test_group内容:
./test1
./test1000
./test10000
if (testfile[strlen(testfile) - 1] == '\n')
testfile[strlen(testfile) - 1] = '\0';
这一段很重要,因为test_group里的文件路径内容每一行后面都有一个换行符,需要删除才能不影响接下来的open操作,不然会出现段错误