Variable / Dynamic Paths in C

This is a discussion on Variable / Dynamic Paths in C within the C Programming forums, part of the General Programming Boards category; Hi all, Im trying to use a variable as a path in C. For instance, the user enters a directory ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    8

    Variable / Dynamic Paths in C

    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,

    Code:
    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

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up

    I don't know why you are doing these many things to get the files in the directory and print the file names with its full path.

    You can do this by opendir and readdir functions.I have developed this code in linux environment.

    Code:
    
    
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include<unistd.h>
    #include <dirent.h>
    DIR *db;
    struct dirent *dirp;
    
    main()
    {
            char directory[50];
            printf("Enter the directory name : ");
            scanf("%s",directory);
    if((db=opendir(directory))!=NULL)
    {
            while((dirp=readdir(db))!=NULL)
            {
                    printf("file name : %s/%s \n",directory,dirp->d_name);
            }
    }
    else
    {
            printf("Can not open directory\n");
    }
    }
    Thanks
    Last edited by karthigayan; 02-27-2010 at 04:25 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    UK
    Posts
    8
    Hi, thanks for your reply and example code.

    Great suggestions, but i'd like to read in each file in turn as an array and search for a string of characters before moving onto the next. I've got that part of the code working with is just the sub function part but can't get create a variable using another variable + *.* at the end.

    Is there a similar way for me to use the same methodology with your method shown in example code?

    Thank you for your time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21