這可能很簡單,但我不明白爲什麼我不能從這個char *中獲得值。C - char *爲空?
這裏是我的問題:
static char* DIR_ENTRY_PATH = NULL;
...
while (1) // infinite loop
{
// accept a client connection
clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);
if (fork() == 0) // Create child proc to get dir entry info
{
//read dir entry info
readInfo(clientFd);
printf("dpath: %s\n", DIR_ENTRY_PATH); //prints out the the correct value (DIR_ENTRY_PATH set in readInfo)
int test = 1;
//get dir entry info and write back
DIR *dir;
struct dirent *entry; //pointer to dir entry
struct stat stbuf; //contains file info
if((dir = opendir(DIR_ENTRY_PATH)) == NULL){ //make sure entry is valid
printf("error with dirent\n");
exit(1);
}
else{
printf("gathering directory entry info...\n");
while((entry = readdir(dir)) != NULL){
char *entryname = entry->d_name;
printf("path: %s\n", DIR_ENTRY_PATH); /*prints nothing out.. */ - PROBLEM IS HERE
printf("int: %d\n", test); //*prints 1 out.. */
...
readInfo():
//reads info from the client
void readInfo(int fdesc){
int fd = fdesc;
char str[200];
readLine(fd, str); //read line in from socket
DIR_ENTRY_PATH = str;
printf("received path: %s\n", DIR_ENTRY_PATH); //displays correct value
}
//reads a single line
int readLine(int fdesc, char *strng){
int fd = fdesc;
char *str = strng;
int n;
do{
n = read(fd,str, 1); //read a single character
}while(n > 0 && *str++ != 0);
return (n>0); //return false if eoi
}//end readLine
爲什麼我能得到測試int的值,但不是dir_entry_pa TH?謝謝你的幫助。
Omg。如此愚蠢。謝謝。 – Jordan