0

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

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Salem
Featured Replies
  • 1
    Salem 5,138   6 Years Ago

    LOL@Dave :) [url]http://cboard.cprogramming.com/c-programming/124248-variable-dynamic-paths-c.html[/url] Now go there and recommend a Daniweb FAQ for completeness :D Read More

0

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".

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.