Open files from current directory using C ++ wildcard

Is it possible to open files from a given directory in C / C ++ using wildcards?

In the sense, if I want to open files ending in "to (node_id)" from the current current directory in my program.

Example. I need to open files whose file names end in "to4". Desired result: it should open all files in the current directory with file names that end in "to4". If the files from0to4, from1to4, from2to4 and from3to4 exist in the current directory, they must be opened.

If this is not possible in C / C ++, is there any other programming language that allows this?

+3
source share
2 answers

Not in standard C ++; you will need to use OS-specific function calls or a file system library such as Boost.Filesystem.

In any case, you can get a list of files in a given directory and iterate over the list and open only those that match your parameters.

+4
source

You can do this with C and C ++. On Windows, you can use [ FindFirstFileand FindNextFile] ( http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx) .

Your platform may already do this for you. If you are using Unix, say that you are running the following command:

$ ./myprog *to4

The shell first extends the wildcard and then executes myprogwith extended arguments equivalent to

$ ./myprog from0to4 from1to4 from2to4 from3to4

.

, : Perl:

#! /usr/bin/perl

foreach my $file (<*to4>) {
  open my $fh, "<", $file or die "$0: open $file: $!\n";

  while (<$fh>) {
    print "$file: $_";
  }
}

, , , :

$ for i in 0 1 2 3; do
> echo file $i > from${i}to4
> done

$ ./opento4s
from0to4: file 0
from1to4: file 1
from2to4: file 2
from3to4: file 3
+4

Source: https://habr.com/ru/post/1724900/


All Articles