Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The implementation of ls command in c language

C
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char* argv[])
{
    DIR *mydir;
    struct dirent *myfile;
    struct stat mystat;

    mydir = opendir(argv[1]);
    while((myfile = readdir(mydir)) != NULL)
    {
        stat(myfile->d_name, &mystat);
        printf("%d",mystat.st_size);
        printf(" %s\n", myfile->d_name);
    }
    closedir(mydir);
}


the error occured is:
./ls_command: line 7: syntax error near unexpected token `('
./ls_command: line 7: `int main(int argc, char* argv[])'

please help...
Posted
Updated 23-Nov-14 0:13am
v3
Comments
CPallini 23-Nov-14 6:58am    
Your code compiles fine (just a couple of warnings) and executes fine on my system.
Member 10099183 23-Nov-14 8:10am    
so it's giving error on my machine
??
PIEBALDconsult 24-Nov-14 10:19am    
So what are sys/types.h and sys/stat.h ? Are they yours? If so, shouldn't they be included with quotes, rather than angle-brackets?
Additionally, if they are yours, they should probably be included after the others.
Member 10099183 24-Nov-14 10:26am    
they are also not mine all the headers files included are inherited from system
PIEBALDconsult 24-Nov-14 12:37pm    
Comment out the entire main and see what happens. Then comment out includes until you track down which one has a problem.

1 solution

Check your header files: I suspect that the fault is in "dirent.h" and it's being spotted in the main code.
To check, comment out the #include line and recompile - it won't compile correctly, but the error will move to the
C++
DIR *mydir;
line in all probability.

Then look closely at the content of the .h file, and see if you can spot what you mistyped - it may be as simple as a missing ";".
 
Share this answer
 
Comments
Member 10099183 23-Nov-14 10:48am    
It doesn't make any difference all header file are in bilt-in in OS...
PIEBALDconsult 24-Nov-14 12:55pm    
"Eliminate the impossible..." -- Sherlock Holmes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900