A newbie plz help me out(C programming in linux)
I am stuck in understanding a code given in one of the leading books on linux programming, the program is to print the content of a dir. the place I dont understand , I marked with `?`. I use FC4. Here goes the code :
#include<unistd.h> #include<stdio.h> #include<dirent.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h> void printdir(char *dir, int depth)//What depth refer? { DIR *dp; struct dirent *entry; struct stat statbuff; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open dir %s\n",dir); return; } chdir(dir); while((entry=readdir(dp)) != NULL) /* what about initialisation of dp*/ { lstat(entry->d_name,&statbuff); /* what about initialisation of entry ?*/ if(S_ISDR(statbuf.st_mode)) { if(strcpm(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0 ) printf("%*s%s/\n",depth,"",entry->d_name); printdir(entry->d_name,depth+4); } else printf("%*s%s\n",depth,"",entry->d_name); } chdir(".."); closedir(dp); } int main() { printdir("/home",0); exit(0); } plz help me.... can anyone provide me a link from where I can start linux programming. |
The initializations you're referring to are assignments which occur inside the conditional "if" and "while" statements. In C, the binary assignment operator ("=") returns the value of the right-hand side, in addition to performing the assignment to the left-hand side as a side-effect. Thus, you commonly see things like "while ((c = fgetc(fp)) != EOF)" where the result of fgetc(fp) is assigned to c and then compared against EOF.
|
Code:
DIR *dp; It is helpful to the readers of your questions if you place [ CODE] [ /CODE] wrappers around your source code. --- rod. |
Something else to think about...
Even though C/C++ provide these "helpful" constructions, they are confusing (as you saw demonstrated), and you might well elect not to use them. In any modern compiler, they won't make any difference in the final code. What matters .. what matters most above all .. is that your code is clear, expressive of what you want to do, and maintainable (long after you've been smooshed by a bread-truck .. or, if you prefer, long after you've cashed in your stock-options and retired to Pango-Pango where you are now idly using solid gold coins as poker-chips). So, if you find constructions like these confusing, don't use them in your code (but do be prepared to see them in somebody else's). When you write your code, put meaningful comments in it. Try to write as little of your code as possible ;) at 1:30 AM. (Wait until at least 3:30 AM...) :D |
Quote:
|
t_v, you pick a mean nit :-) and many would feel insulted by your exquisite pedantry. However, you are quite correct. I inadvertantly used the term 'initialize', because it was the word used by the original poster.
--- rod. |
Quote:
|
All times are GMT -5. The time now is 02:45 AM. |