How to fopen("dun*.dat","r")?

How to fopen("dun*.dat","r")?

Post by Robert Kl » Thu, 28 Sep 1995 04:00:00




Quote:>Hi,
>   Please give me some idea how to write a C program to read multple
>using wildcard character, e.g. dun*.dat. I have tried with
>fopen("dun*.dat","r") but it did not work.

Because fopen() wants COMPLETE filenames, not wildcarded ones. You'd
have to make a function which will scan the current directory for files
mathing that particular expression.

                                                                robert

 
 
 

How to fopen("dun*.dat","r")?

Post by Kent Marfle » Thu, 28 Sep 1995 04:00:00



Quote:>Hi,
>   Please give me some idea how to write a C program to read multple
>using wildcard character, e.g. dun*.dat. I have tried with
>fopen("dun*.dat","r") but it did not work.
>   Your help would be appreciated:)
>Regards,
>Pisut T.

To read multiple files you need to get the file names, and then open
a file pointer for each file (or read them one at a time or whatever :)
Read the man pages for readdir (or opendir, directory ...)

From the man page on readdir() (section 3c on my Solaris box):
...
EXAMPLES                                                          
     Here is a sample program that prints the names  of  all  the
     files in the current directory:                              

          #include <stdio.h>                                      
          #include <dirent.h>                                    

          main()                                                  
          {                                                      
               DIR *dirp;                                        
               struct dirent *direntp;                            

               dirp = opendir( "." );                            
               while ( (direntp = readdir( dirp )) != NULL )      
                    (void)printf( "%s\n", direntp->d_name );      
               (void)closedir( dirp );                            
               return (0);                                        
          }                                                      

You should also check out the man page for dirent (section 4).

I think this should be the same on other UNIX variants.

Hope it helps,
Kent
--


CU Services, Systems Management | voice     (403) 420-4115
Edmonton, Alberta, Canada       | fax       (403) 420-3737

 
 
 

How to fopen("dun*.dat","r")?

Post by john eng » Sat, 07 Oct 1995 04:00:00



: >Hi,
: >   Please give me some idea how to write a C program to read multple
: >using wildcard character, e.g. dun*.dat. I have tried with
: >fopen("dun*.dat","r") but it did not work.
: >   Your help would be appreciated:)
: >Regards,
: >Pisut T.

: To read multiple files you need to get the file names, and then open
: a file pointer for each file (or read them one at a time or whatever :)
: Read the man pages for readdir (or opendir, directory ...)

: From the man page on readdir() (section 3c on my Solaris box):
: ...
: EXAMPLES                                                          
:      Here is a sample program that prints the names  of  all  the
:      files in the current directory:                              
:                                                                  
:           #include <stdio.h>                                      
:           #include <dirent.h>                                    
:                                                                  
:           main()                                                  
:           {                                                      
:                DIR *dirp;                                        
:                struct dirent *direntp;                            
:                                                                  
:                dirp = opendir( "." );                            
:                while ( (direntp = readdir( dirp )) != NULL )      
:                     (void)printf( "%s\n", direntp->d_name );      
:                (void)closedir( dirp );                            
:                return (0);                                        
:           }                                                      
:                                                                  
: You should also check out the man page for dirent (section 4).

: I think this should be the same on other UNIX variants.

Wow... I didn't know C had commands like `readdir' and what have you - my
book on C doesn't mention them *anywhere*. Still, I did a `man readdir' and
there it was... however `man dirent' told me there was no entry for dirent,
as did man 4 dirent. (I'm using OSF3.2 on a DEC3000 box)

To my point. All the examples given deal with either a single named file,
or all the files in a dir. What isn't clear (to me at least) is whether this
method allows you to specify *some* of the files in the directory using a
wildcard as the original poster asked. I suspect it isn't, although I haven't
tried. I have a way to do it, although it is cheesy - but hey, I only just
learnt about readdir. Anyway, here's what I do...

ls -l dun*.dat | grep -v filelist.dat | awk '{print $9}' > /tmp/filelist.dat

This creates a file in /tmp with all the filenames you're interested in in
/tmp. Use a system() call to use this in a C prog. Then, fopen /tmp/filelist.dat,
and each line you read will contain one of the files you want to open.
fopen() each file in turn and do what you like...

like I say, its cheesy but it *does* work with wildcards... If theres a
better way, I could do with learning it myself.


 
 
 

1. How to fopen("dun*.dat","r")?


Depends on what you want to do.  If you want to read from the aggregate of
all such files as a single stream, popen( "cat dun*.dat", "r" ) should do
it.

  FILE *pf;
  char szBuff[ 1025 ];
#define MAXBUFF (sizeof( szBuff ) - 1)

  pf = popen( "cat dun*.dat", "r" );
  if (pf) {

        while (fgets( szBuff, MAXBUFF, pf )) {
                fputs( szBuff, stdout );
        } /* not end of pipe */

        pclose( pf );

  } /* Opened pipe successfully - _don't ever_ leave one open! */

-Henry Groover

2. dos contemporaries...cpm what else?

3. fopen("~/.prefs.rc", "r"); can't find file

4. NetBSD for i80960?

5. Type "(", ")" and "{", "}" in X...

6. ad filering proxy

7. GETSERVBYNAME()????????????????????"""""""""""""

8. is it possile to install SUSE by ftp

9. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

10. question obout "vmstat" and "sar -r" "swap -l"

11. where can I ftp "metaconfig" (generator of "Configure")?

12. Definition of "word" in zsh (e.g., "backward-kill-word")

13. pccardd[49]: No card in database for "(null)"("(null)")