当我运行这个程序时。文件描述符返回-1
值,因此程序终止。我不知道为什么会这样。因为 pid 值正是我要打开的文件的名称。
const char *psyn = "product/productId:";
char line[100];
pFile = fopen ("pids.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while(fgets (line , sizeof(line) , pFile))
{
if (strstr(line, psyn) == line)
{
leng = strlen(line);
if( line[leng-1] == '\n' )
line[leng-1] = 0;
if( line[0] == '\n' )
line[0] = 0;
pid = line+strlen(psyn)+1;
strcat(pid,t);
leng = strlen(pid);
if( pid[leng-1] == '\n' )
pid[leng-1] = 0;
fd = open(pid, O_RDWR, 0644);
if (fd == -1)
cout<<"eror in file \n";
else { //.. rest of the program}