【发布时间】:2014-06-07 18:31:21
【问题描述】:
当我使用 open() 作为
int ff=open("/home/user/desktop/bla/test",O_RDONLY);
效果很好。
但是当我使用字符串作为(存储在字符串中的相同路径)路径时,它不起作用。
int ff=open(string,O_RDONLY);
这是为什么?
这是我的全部代码。我糊涂了。我知道它应该工作。但我没有。我找不到错误。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
void bla(char *);
int main(void)
{
// int i;
FILE * fp=fopen("path","r");
char line[256];
while( fgets(line,255,fp)!=NULL){
bla(line);
}
return 0;
}
void bla(char * line){
int status;
printf("%s",line);
pid_t pid=fork();
char string[256];
if(pid==0){
pid=wait(&status);
int ff=open(line,O_RDONLY);
if(ff<0){
printf("\topen error!\n\n");
return;}
int ret=read(ff,string,255);
if(ret<0){
printf("read error!\n");
return;}
printf("%s",string);
close(ff);
exit(0);
}
if (pid>0){
return;
}
}
在 bla 函数中,如果我将 'line' 替换为路径,它可以工作。我用 printf 来确定。路径是一样的(看起来一样)。
【问题讨论】:
-
你能不能说明一下'string'是如何定义和初始化的?也许:char *string="/home/user/desktop/bla/test" ???
-
尝试使用
strerror打印errno。
标签: c path directory system-calls