There is a local diretory alled directory which contins only one file, I want to know the name of the file using c.. here is what I have got from Net:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
void getFile (char *);
main() {
char s[50];
getFile(s);
printf("%s", s);
}
void getFile (char *file)
{
char dir[100] = "directory/";
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir)) == NULL) {
perror("\nUnable to open directory.");
exit(0);
}
dirp = readdir(dp);
strcpy(file,dirp->d_name);
closedir(dp);
}
There is file there in the directory though I get output as:
Is there any other good way to do this? OR what is the problem with this code?
Note: the folder contains only one file.