Hi all,

Im trying to use a variable as a path in C. For instance, the user enters a directory and this is used with the '*.*' notation attached so the program loops all files in the directory. I've got the PHP part of this code working in a separate part of the program but dont seem to be able to get it working in C. I've attached the example code below, any ideas?

Thank you for your time,

const char directory[50];

printf("Please enter directory to open: \n");
scanf("%s@, directory);

const char directoryfile[60] = "directory\\*.*";

do
{
...

If the user entered C:\Test as the directory
then
the directoryfile should become "C:\Test\file1.doc" etc.

Thanks again

Recommended Answers

All 3 Replies

How to get all the files in a directory depends on the operating system. In MS-Windows use FindFirstFile() and FindNextFile() found in windows.h. If *nix use OpenDir() and ReadDir(). Then of course you may have to contend with subdirectories. The easiest way to do that is with recursion.

Use strcat() to append text to the user's input variable. Make sure the buffer is large enough to hold all the text.


>>scanf("%s@, directory);
Scanf() is very bad because (1) will not allow spaces in what you type -- many directories in MS-Windows contain spaces, and (2) allows you to enter more characters than the buffer can hold. Replace scanf() with fgets()

>>const char directory[50];
Remove the const keyword because you can't use it in scanf() or fgets(). const means "not changeable".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.