如何在我的C程序中验证当前有四个终端是打开的?

时间:2016-04-22 00:19:41

标签: c linux

所以我要说这是为了我班上的家庭作业项目。我应该确定用户在运行程序之前打开了四个终端窗口,我必须通过确定是否可以从/ dev / pts /打开四个终端号缓冲区来执行此操作。然后我必须保存前四个缓冲区,这样我就可以再次打开它们来写入终端。我知道如何使用fopen打开文件,但我的问题是甚至不再打开的终端仍然显示并且可以访问。我知道它非常不满意要求作业帮助,但我已经在这工作了几个小时,我不想为它写的我只想要一些方向。如何使用我必须使用的方法检查是否有四个端子打开?这也是我的代码,所以也许你们中的一个人可以看到我做错了什么。

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 100

int main(){
    int i, ptsNum[4], ptsCount = 0;
    FILE *fp;
    char ptsName[20];

    for(i = 0; i < 20; i++){
        // Append the terminal number to the end of the buffer name
        sprintf(ptsName, "/dev/pts/%d", i);
        // Try to open the file
        if((fp = fopen(ptsName, "r")) != NULL){
            // Save the terminal number if the buffer exists
            ptsNum[ptsCount] = i;
            ptsCount++;
            fclose(fp);
        }
    }

return 0;
}

1 个答案:

答案 0 :(得分:0)

好吧,你试着打开终端,但是一旦你计算了它们,就fclose(3)它们,所以如果你想让它们打开,不要在最后fclose(3)结束。循环。