when looking for a file type that can be opened and read (the contents within the file) or not. How to determine what type of file is it?
using this guild line
what type of file would I be lookng for to determine between an image file and a file with contents within it to be opened via fopen?
reg file returns for both as a match for reg file
C:
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
int isDirectory(const char *path)
{
struct stat status;
stat(path, &status);
if (S_ISDIR(status.st_mode))
return 1;
return 0;
}
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
int main (int argc, const char **argv) {
if (argc < 2) {
printf("Need a path to dir or file\n");
return EXIT_FAILURE;
}
if (isDirectory(argv[1])) {
printf("is dir %s\n", argv[1]);
}
else
{
printf(" is not a dir %s\n", argv[1]);
}
if (is_regular_file(argv[1])) {
printf("is file %s\n", argv[1]);
}
else
{
printf(" is not a file %s\n", argv[1]);
}
return EXIT_SUCCESS;
}
what type of file would I be lookng for to determine between an image file and a file with contents within it to be opened via fopen?
reg file returns for both as a match for reg file
Code:
userx@FreeBSD.net:/usr/home/userx/test
$ ./a.out /home/userx/SavedImages
is not a dir /home/userx/SavedImages
is file /home/userx/SavedImages
//
userx@FreeBSD.net:/usr/home/userx/test
$ ./a.out /home/userx/wallhaven-42ok96.jpg
is not a dir /home/userx/wallhaven-42ok96.jpg
is file /home/userx/wallhaven-42ok96.jpg