C++ Help

aegisalpha

Contributor
Veteran X
I'm wanting to write a small program to cycle through all files of a certain type in a directory and store the file names (including the path) as elements in an array of strings. I'm guessing I should be able to use the FindFirstFile() and FindNextFile()functions to do this, but I really don't have any experience with these functions.

Can anyone help me a little with this please?

Thanks in advance
 
I would use system(dir/blah foo > bar.txt)

then read from bar.txt

But there's probably a better way to do it.
 
Those are win32 API calls, not in any way associated with C++.

You could find much easier alternatives that are more portable, as I assume you're working with console, right?
 
It doesn't need to be portable. Basically I have something like 250 mappoint files that need to be updated regularly with some data. I want to make it so they can add maps and still use this program to batch update them all without having to worry about adding the new filenames to a txt file.
 
aegisalpha said:
It doesn't need to be portable. Basically I have something like 250 mappoint files that need to be updated regularly with some data. I want to make it so they can add maps and still use this program to batch update them all without having to worry about adding the new filenames to a txt file.

Then my solution should work.
 
use stat()
Code:
System Calls                                              stat(2)



NAME
     stat, lstat, fstat - get file status

SYNOPSIS
               <sys/types.h>
               <sys/stat.h>

     int stat(const char *path, struct stat *buf);

     int lstat(const char *path, struct stat *buf);

     int fstat(int fildes, struct stat *buf);

DESCRIPTION
     The stat()  function  obtains  information  about  the  file
     pointed  to  by  path. Read, write, or execute permission of
     the named file is not required, but all  directories  listed
     in the path name leading to the file must be searchable.

     The lstat() function  obtains  file  attributes  similar  to
     stat(),  except  when  the named file is a symbolic link; in
     that case lstat() returns information about the link,  while
     stat()  returns  information  about the file the link refer-
     ences.

     The fstat() function obtains information about an open  file
     known  by  the  file descriptor fildes, obtained from a suc-
     cessful open(2),  creat(2),  dup(2),  fcntl(2),  or  pipe(2)
     function.

     The buf argument is a pointer to a stat structure into which
     information  is placed concerning the file. A stat structure
     includes the following members:

     mode_t   st_mode;     /* File mode (see mknod(2)) */
     ino_t    st_ino;      /* Inode number */
     dev_t    st_dev;      /* ID of device containing */
                           /* a directory entry for this file */
     dev_t    st_rdev;     /* ID of device */
                           /* This entry is defined only for */
                           /* char special or block special files */
     nlink_t  st_nlink;    /* Number of links */
     uid_t    st_uid;      /* User ID of the file's owner */
     gid_t    st_gid;      /* Group ID of the file's group */
     off_t    st_size;     /* File size in bytes */
     time_t   st_atime;    /* Time of last access */
     time_t   st_mtime;    /* Time of last data modification */
     time_t   st_ctime;    /* Time of last file status change */
                           /* Times measured in seconds since */
                           /* 00:00:00 UTC, Jan. 1, 1970 */
     long     st_blksize;  /* Preferred I/O block size */
     blkcnt_t st_blocks;   /* Number of 512 byte blocks allocated*/

     Descriptions of structure members are as follows:

     st_mode
           The mode of the file  as  described  in  mknod(2).  In
           addition  to  the modes described in mknod(), the mode
           of a file may also be S_IFLNK if the file  is  a  sym-
           bolic link. S_IFLNK may only be returned by lstat().

     st_ino
           This field uniquely identifies the  file  in  a  given
           file  system.  The  pair   st_ino and  st_dev uniquely
           identifies regular files.

     st_dev
           This field uniquely identifies the  file  system  that
           contains  the  file. Its value may be used as input to
           the ustat() function  to  determine  more  information
           about this file system. No other meaning is associated
           with this value.

     st_rdev
           This field should be used only by administrative  com-
           mands. It is valid only for block special or character
           special files and only has meaning on the system where
           the file was configured.

     st_nlink
           This field should be used only by administrative  com-
           mands.

     st_uid
           The user ID of the file's owner.

     st_gid
           The group ID of the file's group.

     st_size
           For regular files, this is the address of the  end  of
           the file. For block special or character special, this
           is not defined. See also pipe(2).

     st_atime
           Time when file data was last accessed. Changed by  the
           following   functions:   creat(),   mknod(),   pipe(),
           utime(2), and read(2).

     st_mtime
           Time when data was last modified. Changed by the  fol-
           lowing  functions:  creat(), mknod(), pipe(), utime(),
           and write(2).

     st_ctime
System Calls                                              stat(2)



           Time when file status was last changed. Changed by the
           following   functions:   chmod(),   chown(),  creat(),
           link(2),  mknod(),  pipe(),  unlink(2),  utime(),  and
           write().

     st_blksize
           A hint as to the "best" unit size for I/O  operations.
           This field is not defined for block special or charac-
           ter special files.

     st_blocks
           The total number of physical blocks of size 512  bytes
           actually  allocated on disk. This field is not defined
           for block special or character special files.
 
x66 said:
Those are win32 API calls, not in any way associated with C++.

You could find much easier alternatives that are more portable, as I assume you're working with console, right?
Beat me to it. I have a book on Unix programming and the first program is reading a directory. Anyway, most the methods of things such as this are platform specific.
 
You don't want to deal with the subtle differnces in the pseudo-posix implementations of the file functions on various platforms, or the misdocumentation for that matter ;). Either listen to daerid and find some half-way decent class library like Boost, or go check out the msdn pages for FindFirstFile if you don't mind writing win32 only code.

Also, it almost sounds like c++ isn't quite the language for task. You might want to look into more of a processing language like perl.


However to get you started, here are some win32 file links (the api is pretty easy to use)...

MSDN File Management Functions:
http://msdn.microsoft.com/library/d...-us/fileio/base/file_management_functions.asp

MSDN FindFirstFile Example: (Note that the first param CAN contain wildcards)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findfirstfile.asp


Have fun.
 
Back
Top