listing files in a folder using C in Windows - c

I want to list the files in this folder "C:\home\WORK\Desktop\Communication". There are ten files in this folder. My code has no error but it's not printing anything. What is my mistake ?
#include<stdio.h>
#include<Windows.h>
#include<string.h>
int main(int argc,char *argv[])
{
char path[]="C:\\home\\WORK\\Desktop\\Communication";
strcat_s(path,sizeof(path),"\\*");
WIN32_FIND_DATA fdata;
HANDLE hFind =INVALID_HANDLE_VALUE;
int numberOfFiles=0;
char *files[10];
hFind = FindFirstFile(path,&fdata);
while((FindNextFile(hFind,&fdata))!=0)
{
files[numberOfFiles]=fdata.cFileName;
numberOfFiles++;
printf("%s\n",files[numberOfFiles]);
}
FindClose(hFind);
return 0;
}

There are a few things wrong with your code.
strcat_s can't append "\\*" to your path char array. The buffer only has enough room for storing the string literal.
I'm uncomfortable with letting you declare a buffer files that has only enough memory to fit all the file names. What happens if you add one more file? Then the buffer is overrun. However, it will still technically work in this scenario.
This line printf("%s\n",files[numberOfFiles]); is undefined behavior. You incremented numberOfFiles to a location in the array that has not been initialized, so it's not going to print the file name.
When you call FindClose, you invalidate all of those pointers that you stored in files. You can no longer use them. You need to copy the string to a new buffer.
The following code works.
#include<stdio.h>
#include<Windows.h>
#include<string.h>
int main(int argc,char *argv[])
{
char path[] = "C:\\home\\WORK\\Desktop\\Communication\\*.*";
//strcat_s(path,sizeof(path),"\\*");
WIN32_FIND_DATA fdata;
HANDLE hFind =INVALID_HANDLE_VALUE;
int numberOfFiles=0;
char* files[10]; /* you may want to expand this buffer */
hFind = FindFirstFile(path,&fdata);
while((FindNextFile(hFind,&fdata))!=0)
{
size_t len = strlen(fdata.cFileName);
files[numberOfFiles] = malloc(len + 1 * sizeof*files); // len + 1 for null-terminator
strcpy_s(files[numberOfFiles], len, fdata.cFileName);
printf("%s\n",files[numberOfFiles]);
numberOfFiles++; /* increment this AFTER you print files[numberOfFiles] */
}
FindClose(hFind);
for(int i = 0; i < (sizeof(file)/sizeof(*file)); ++i) {
free(file[i]);
}
return 0;
}

Seems, you should move
numberOfFiles++
past
printf("%s\n",files[numberOfFiles]);
Or
while((FindNextFile(hFind,&fdata))!=0)
{
files[numberOfFiles]=fdata.cFileName;
printf("%s\n", files[numberOfFiles++]);
}
Which is same as
while((FindNextFile(hFind,&fdata))!=0)
{
printf("%s\n", files[numberOfFiles++] = fdata.cFileName);
}
I don't know WinAPI well, but I feel, that all files will contain invalid pointers after FindClose(hFind) as well as all elements of files will point to *(fdata.cFileName) which will be released in FindClose(hFind). In other words, as I understand this, you should copy (or duplicate) fdata.cFileName into files[i] on each iteration.

Clear fdata struct after declaring it:
memset( &fdata, 0, sizeof fdata )

Related

How to read all files in the directory and store them in the pointer in the C language program?

Mingw x86_64 v11.2.0
Windows 10 21H2
My current problem is that this function can normally read files and folders in the directory, but if it is saved in the pointer and printed on the screen, a single file will appear repeatedly.
These are the files in the directory.
.
..
main.c
main.exe
README.md
test.txt
The following is the source code I wrote:
#include "../DeleteCompletely.h"
#include <dirent.h>
#define TotalNumber 4096
#define TotalFileNameLen 4096
typedef struct dirent DIRENT;
typedef struct {
DIR *dir_ptr;
DIRENT *dirent_ptr;
char **pathSet;
size_t number;
} snDIR;
static snDIR *getAllFile(const char *path)
{
snDIR *dirSet = (snDIR *)malloc(sizeof(snDIR));
dirSet->dir_ptr = opendir(path);
size_t loopIndex;
dirSet->pathSet = (char **)malloc(sizeof(char **) * TotalNumber);
if(dirSet->dir_ptr) {
dirSet->number = 0;
loopIndex = 0;
while ((dirSet->dirent_ptr = readdir(dirSet->dir_ptr)) != NULL) {
dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen);
dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;
dirSet->number++;
loopIndex++;
}
closedir(dirSet->dir_ptr);
}
return dirSet;
}
int main(int argc, char **argv)
{
snDIR *set = getAllFile(".");
for(size_t x = 0; x < set->number; ++x) {
printf("%s\n", set->pathSet[x]);
}
free(set);
return 0;
}
The problem are these assignments:
dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen);
dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;
The first make dirSet->pathSet[loopIndex] point to one location. The second make it point somewhere completely different.
Instead of the second assignment you need to copy the string:
strcpy(dirSet->pathSet[loopIndex], dirSet->dirent_ptr->d_name);
You're also wasting quite a lot of memory by always allocating a large amount or memory. The likelihood of the string being that large are rather small. Instead use the string length as the base size:
dirSet->pathSet[loopIndex] = malloc(strlen(dirSet->dirent_ptr->d_name) + 1);
// +1 for the null terminator

Reading a .tiff file in c from inside zip

I have multiple tiff images stored inside a zip file and would like to read their pixel values in c. I am very new to c so please forgive the dodgy code. I have got to the point where I have a char * with the contents of the tiff file but can't seem to work out how to now process that with libtiff (or something similar). libtiff seems to require that I pass TIFFOpen a filename to open. I could write the tiff to a temporary file but it feels like there must be a more efficient way.
So far I have:
#include <stdlib.h>
#include <stdio.h>
#include <zip.h>
#include <string.h>
int main()
{
//Open the ZIP archive
int err = 0;
struct zip *z = zip_open("test.zip", 0, &err);
// Determine how many files are inside and iterate through them
int num_files = zip_get_num_entries(z, 0);
printf("%u\n", num_files);
int i;
for (i=0; i < num_files; i++)
{
const char * filename;
filename = zip_get_name(z, i, 0);
// If the file name ends in .tif
if (strlen(filename) > 4 && !strcmp(filename + strlen(filename) - 4, ".tif"))
{
printf("%s\n", filename);
// Get information about file
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, name, 0, &st);
printf("%lld\n", st.size);
// Allocate memory for decompressed contents
char *contents;
contents = (char *)malloc(st.size);
// Read the file
struct zip_file *f = zip_fopen(z, filename, 0);
zip_fread(f, contents, st.size);
zip_fclose(f);
// Do something with the contents
// Free memory
free(contents);
}
}
//And close the archive
zip_close(z);
}
EDIT: My question is similar to this one but the accepted answer there relates to c++ and I'm not sure how to translate it to straight c.

C, looping array of char* (strings) does't work. Why?

I have problem with my array of char*-
char *original_file_name_list[500];
while(dp=readdir(dir)) != NULL) {
original_file_name = dp->d_name;
original_file_name_list[counter] = original_file_name;
printf("%s\n",original_file_name_list[0]);
printf("%d\n",counter);
counter++;
}
The problem is, that it prints all files fine. It should print only first file, right?
And if I try printf("%s\n",original_file_name_list[1]); It doesn't work , which means that it is writing only in 1st string. Any idea why?
edit: There is no syntax error due to compiler.
You're not copying the string at all - also your file_name_list array hasn't enough space for a list of filenames - just for a list of pointers. But dp->d_name is just a pointer to a char* - you can't know for how long the memory behind the pointer is valid. Because of that you have to make a copy for yourself.
#include <string.h>
#include <dirent.h>
int main(int argc, char** argv){
char original_file_name_list[50][50];
size_t counter = 0;
while(dp=readdir(dir)) != NULL) // does work fine (ordinary reading files from dir)
{
size_t len = strlen(dp->d_name);
if(len >= 50) len = 49;
strncpy(original_file_name_list[counter], dp->d_name, len);
original_file_name_list[counter][len] = '\0';
printf("%d\n",counter);
counter++;
}
printf("%s\n",original_file_name_list[1]); // <- will work if you have at least 2 files in your directory
return 0;
}
I'm not sure about purpose of counter2 (I have replaced it with counter) but I can propose the following code with strdup() call to store the file names:
char *original_file_name_list[500] = {0}; // it is better to init it here
while(dp=readdir(dir)) != NULL) {
original_file_name_list[counter] = strdup(dp->d_name); // strdup() is ok to use
// here, see the comments
printf("%s\n%d\n",original_file_name_list[counter], counter);
counter++;
}
/* some useful code */
/* don't forget to free the items of list (allocated by strdup(..) )*/
for (int i = 0; i < 500; ++i) {
free(original_file_name_list[i]);
}

How can I check if file is text (ASCII) or binary in C

I need to write C code that checks to see if a file is text(ASCII) or Binary
Could someone help?
Thanks
Typical method is to read the first several hundred bytes and look for ASCII NUL.
If the file contains NUL, it is definitely a binary file. Most binary files do contain NUL bytes, but text files should never contain NUL bytes.
#include <string.h>
bool is_binary(const void *data, size_t len)
{
return memchr(data, '\0', len) != NULL;
}
Be warned that this is a heuristic. In other words, it will be wrong sometimes.
Read all characters and see if all of them are ASCII, that is, with codes from 0 to 127 inclusive.
Some tools determine whether a file is a text file or a binary file by just checking whether or not it has any byte with code 0.
Clearly, if you apply both of these methods, you will get different results for some files, so, you have to define what it is exactly that you're looking for.
You can use libmagic. The code below will show you roughly the way the "file" command does it. (The code below is quick and dirty -- it probably needs to be cleaned up.)
#include <string.h>
#include <magic.h>
#include <stdio.h>
//------------------------------------------------------------------------------
struct magic_set * prep_magic(int flags)
{
struct magic_set *magic = magic_open(flags);
const char *errstring;
int action = 0;
const char *magicfile = NULL;
if (magicfile == NULL)
magicfile = magic_getpath(magicfile, action);
if (magic == NULL)
{
printf("Can't create magic");
return NULL;
}
if (magic_load(magic, magicfile) == -1)
{
printf("%s", magic_error(magic));
magic_close(magic);
return NULL;
}
if ((errstring = magic_error(magic)) != NULL)
printf("%s\n", errstring);
return magic;
/* END FUNCTION prep_magic */ }
//------------------------------------------------------------------------------
int main(int argc, char **argv)
{
int flags = 0;
struct magic_set *msetptr = NULL;
const char *testfile = (char *)"/etc/motd";
msetptr = prep_magic(flags);
if( msetptr == NULL )
printf("no mset ptr\n");
const char *typer;
typer = magic_file( msetptr, testfile );
printf("typer = %s\n", typer );
return 0;
/* END PROGRAM */ }

Weirdest segmentation fault ever

Ok basically I'm writing a program that takes in two directories and processes them based on what options are supplied. Thing is it's giving me segmentation errors at times when I don't see a problem. The code that is causing the problem is the following:
EDIT: Update the code to include my entire source file
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define OPTLIST "am:npruv"
#define DEFAULT_MOD_TIMES 1
typedef struct
{
/* Boolean toggle to indicate whether hidden files should be
processed or not */
bool processHiddens;
/* Integer number of seconds such that files with modification
times that differ by less than this value are considered the
same */
int timeResolution;
/* Boolean toggle to indicate whether the actual synchronisation
(file creation and overwriting) should be performed or not */
bool performSync;
/* Boolean toggle to indicate whether subdirectories should be
recursively processed or not */
bool recursive;
/* Boolean toggle to indicate whether to print the combined
directory structure or not */
bool print;
/* Boolean toggle to indicate whether modification times and
permissions should be updated or not */
bool updateStatus;
/* Boolean toggle to indicate whether verbose output should be
printed or not */
bool verbose;
/* The name of the executable */
char *programname;
} OPTIONS;
int main(int argc, char *argv[])
{
static OPTIONS options;
//static TOPLEVELS tls;
int opt;
char **paths;
/*
* Initialise default without options input.
* Done the long way to be more verbose.
*/
opterr = 0;
options.processHiddens = false;
options.timeResolution = DEFAULT_MOD_TIMES;
options.performSync = true;
options.recursive = false;
options.print = false;
options.updateStatus = true;
options.verbose = false;
options.programname = malloc(BUFSIZ);
options.programname = argv[0];
/*
* Processing options.
*/
while ((opt = getopt(argc, argv, OPTLIST)) != -1)
{
switch (opt)
{
case 'a':
options.processHiddens = !(options.processHiddens);
break;
case 'm':
options.timeResolution = atoi(optarg);
break;
case 'n':
options.performSync = !(options.performSync);
break;
case 'p':
options.print = !(options.print);
break;
case 'r':
options.recursive = !(options.recursive);
break;
case 'u':
options.updateStatus = !(options.updateStatus);
break;
case 'v':
options.verbose = !(options.verbose);
break;
default:
argc = -1;
}
}
/*
* Processing the paths array for top level directory.
*/
char **tempPaths = paths;
while (optind < argc)
{
*tempPaths++ = argv[optind++];
}
if (argc -optind + 1 < 3)
{
fprintf(stderr, "Usage: %s [-amnpruv] dir1 dir2 [dirn ... ]\n", options.programname);
exit(EXIT_FAILURE);
}
else
{
//processTopLevelDirectories(tls, paths, nDirs, options);
exit(EXIT_SUCCESS);
}
return 0;
}
I have a bash script that when runs does the following:
#!/bin/bash
clear
echo Running testing script
echo Removing old TestDirectory
rm -r ./TD
echo Creating new copy of TestDirectory
cp -r ./TestDirectory ./TD
echo Building program
make clean
make
echo Running mysync
./mysync ./TD/Dir1 ./TD/Dir2
echo Finished running testing script
However if I were to try to run the program manually using the EXACT same command:
./mysync ./TD/Dir1 ./TD/Dir2
I get a segmentation fault between test1 and test2. But if I were to append a / to just any one of the directories, or both, then it works again. Any ideas guys?
EDIT: source_collection.h is basically all of the supporting source codes, so far they have not been implemented yet so they shouldn't cause any problems. OPTIONS is a supplied structure, thus it should be error-free. The current source is still work in progress so there's still some code missing as well as having some codes commented out. Basically at the end of the day the program aims to take in n directories with options and sync the directories.
You need to use strcpy() to copy argv[optind] into your *tempPaths space that you've just allocated.
As it is, you are clobbering (leaking) your allocated memory and then who knows what else goes wrong.
Also, why do you need to make a copy of your arguments? If you don't modify them, you don't need to copy them.
char **tempPaths = paths;
while (optind < argc)
{
printf("test1\n");
// Or use strdup(), but strdup() is POSIX, not Standard C
// This wastes less space on short names and works correctly on long names.
*tempPaths = malloc(strlen(argv[optind])+1);
// Error check omitted!
strcpy(*tempPaths, argv[optind]);
printf("test2\n");
printf("%s\n", *tempPaths);
tempPaths++;
optind++;
}
This assumes you do need to copy your arguments. If you don't need to modify the command line arguments, you can simply use:
char **tempPaths = paths;
while (optind < argc)
*tempPaths++ = argv[optind++];
The code there just vanished as I was editing to remove what was no longer necessary...
It might even be possible to simply set:
char **paths = &argv[optind];
This does away with the loop and temporary variables altogether!
Answering questions in comment
[W]hat do you mean when you say that my allocated memory is leaking?
Your original code is:
*tempPaths = malloc(BUFSIZ);
*tempPaths = argv[optind];
The first statement allocates memory to *tempPaths; the second then overwrites (the only reference to) that pointer with the pointer argv[optind], thus ensuring that you cannot release the allocated memory, and also ensuring that you are not using it. Further, if you subsequently attempt to free the memory pointed to by ... well, it would be paths rather than tempPaths by this stage ... then you are attempting to free memory that was never allocated, which is also a Bad Thing™.
Also I don't particularly get what you mean by "make a copy of your arguments". Are you referring to the two directories used for the command line or for something else?
Your code is making a copy of the arguments (directory names) passed to the program; the revised solution using strdup() (or what is roughly the body of strdup()) makes a copy of the data in argv[optind]. However, if all you are going to do with the data is read it without changing it, you can simply copy the pointer, rather than making a copy of the data. (Even if you were going to modify the argument, if you were careful, you could still use the original data - but it is safer to make a copy then.)
Finally wouldn't char **paths = &argv[optind]; just give me a single directory and that's it?
No; it would give you a pointer to a null-terminated list of pointers to strings, which you could step through:
for (i = 0; paths[i] != 0; i++)
printf("name[%d] = %s\n", i, paths[i]);
Bare minimal working code
As noted in a comment, the basic problem with the expanded crashing code (apart from the fact that we don't have the header) is that the paths variable is not initialized to point to anything. It is no wonder that the code then crashes.
Based on the amended example - with the options processing stripped out:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char **paths;
optind = 1;
paths = &argv[optind];
if (argc - optind + 1 < 3)
{
fprintf(stderr, "Usage: %s dir1 dir2 [dirn ... ]\n", argv[0]);
exit(EXIT_FAILURE);
}
else
{
char **tmp = paths;
while (*tmp != 0)
printf("<<%s>>\n", *tmp++);
}
return 0;
}
And this version does memory allocation:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
optind = 1;
if (argc - optind + 1 < 3)
{
fprintf(stderr, "Usage: %s dir1 dir2 [dirn ... ]\n", argv[0]);
exit(EXIT_FAILURE);
}
else
{
int npaths = argc - optind;
char **paths = malloc(npaths * sizeof(*paths));
// Check allocation!
char **tmp = paths;
int i;
printf("n = %d\n", npaths);
for (i = optind; i < argc; i++)
{
*tmp = malloc(strlen(argv[i])+1);
// Check allocation!
strcpy(*tmp, argv[i]);
tmp++;
}
for (i = 0; i < npaths; i++)
printf("<<%s>>\n", paths[i]);
}
return 0;
}
You have identified that the segfault is occurring on one of these lines:
*tempPaths = malloc(BUFSIZ);
*tempPaths = argv[optind];
It's highly unlikely that there are fewer than argc entries in argv, so we deduce that the problem is the allocation to *tempPaths, so tempPaths cannot be a valid pointer.
Since you don't show how paths is initialised, it's impossible to dig any deeper. Most likely there are fewer than argc members in paths, so your tempPaths++ is causing you to move past the last entry.

Resources