Opening multiple files in C 
Author Message
 Opening multiple files in C

Hi all,

 Is it possible to open multiple files in a directory and read them one by
one from a C program? For example, I want to parse the contents of all
"*.dat "  files in a given directory. How can I do that?

 Thanks,
 Sheshagiri.



Sun, 16 Nov 2003 23:50:12 GMT  
 Opening multiple files in C

Quote:

> For example, I want to parse the contents of all
> "*.dat "  files in a given directory. How can I do that?

You can't, in ISO C. There are no standard provisions for reading a
directory (and devising a truly general one would be harder than it
looks at first glance). See the FAQ:
<http://www.eskimo.com/~scs/C-faq/q19.20.html>.

Richard



Sun, 16 Nov 2003 23:36:38 GMT  
 Opening multiple files in C

Quote:

>  Is it possible to open multiple files in a directory and read them one by
> one from a C program? For example, I want to parse the contents of all
> "*.dat "  files in a given directory. How can I do that?

You can pass the filenames to the program as argv[1] through
argv[n], then have the program open them.  With a unix-like
shell, this is easy, you just type something like
        ./foobar *.dat


Sun, 16 Nov 2003 23:54:08 GMT  
 Opening multiple files in C
 But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?
If so, how do I figure out which file ended where..!

 Please clarify,
 Thanks,
 Sheshagiri.


Quote:

> >  Is it possible to open multiple files in a directory and read them one
by
> > one from a C program? For example, I want to parse the contents of all
> > "*.dat "  files in a given directory. How can I do that?

> You can pass the filenames to the program as argv[1] through
> argv[n], then have the program open them.  With a unix-like
> shell, this is easy, you just type something like
> ./foobar *.dat



Mon, 17 Nov 2003 00:10:23 GMT  
 Opening multiple files in C
Quote:

>  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?

No!

Quote:

> > You can pass the filenames to the program as argv[1] through
> > argv[n], then have the program open them.  With a unix-like
> > shell, this is easy, you just type something like
> > ./foobar *.dat

It's good idea!
paiyi

#include<stdio.h>
int main(int argc, char **argv)
{int i;
 FILF *fp[10000000];

 for (i=1;i<argc;i++) fp[i]=fopen(argv[i],"r");

 /* do your stuff */

 for (i=1;i<argc;i++) fclose(fp[i]);
 return 0;

Quote:
}



Mon, 17 Nov 2003 00:41:50 GMT  
 Opening multiple files in C

Quote:

>  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?

No.  You fopen(argv[1], "r"), then fopen(argv[2], "r"), and so on.

Quote:
> If so, how do I figure out which file ended where..!

What are you talking about?

Quote:



> > >  Is it possible to open multiple files in a directory and read them one
> by
> > > one from a C program? For example, I want to parse the contents of all
> > > "*.dat "  files in a given directory. How can I do that?

> > You can pass the filenames to the program as argv[1] through
> > argv[n], then have the program open them.  With a unix-like
> > shell, this is easy, you just type something like
> > ./foobar *.dat

--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield


Mon, 17 Nov 2003 00:35:42 GMT  
 Opening multiple files in C

Quote:




> > >  Is it possible to open multiple files in a directory and read them one
> by
> > > one from a C program? For example, I want to parse the contents of all
> > > "*.dat "  files in a given directory. How can I do that?

> > You can pass the filenames to the program as argv[1] through
> > argv[n], then have the program open them.  With a unix-like
> > shell, this is easy, you just type something like
> > ./foobar *.dat

>  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?
> If so, how do I figure out which file ended where..!

Don't top post, corrected.

Under unix (and dos I think), if you invoke your program with

./foobar *.dat

the shell will expand the *.dat to file1.dat file2.dat file3.dat etc...
and in your code you can do

#include <stdio.h>

int mian(int argc,char *argv[]){

   FILE file;

   for(argv++;*argv!=NULL;argv++){
      printf("processing %s\n",*argv);
      if((file=fopen(argv,"r"))=NULL){
         fprintf(stderr,"can't open %s\n",argv);
      }else{
         /* do something */
      }
   }

  return 0;

Quote:
}

Tobias.


Mon, 17 Nov 2003 01:14:37 GMT  
 Opening multiple files in C

Quote:


> >  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?
> No!


> > > You can pass the filenames to the program as argv[1] through
> > > argv[n], then have the program open them.  With a unix-like
> > > shell, this is easy, you just type something like
> > > ./foobar *.dat

> It's good idea!
> paiyi

> #include<stdio.h>
> int main(int argc, char **argv)
> {int i;
>  FILF *fp[10000000];

>  for (i=1;i<argc;i++) fp[i]=fopen(argv[i],"r");

>  /* do your stuff */

>  for (i=1;i<argc;i++) fclose(fp[i]);
>  return 0;
> }

Don't you think, that it would be better to process one file
after the other ?

  for(i=1;i<argc;i++)
  {
    f=fopen(argv[i],"rb");

  /* do your stuff */

    fclose(f);
  }

Jirka



Mon, 17 Nov 2003 01:16:46 GMT  
 Opening multiple files in C

Quote:





> > > >  Is it possible to open multiple files in a directory and read them one
> > by
> > > > one from a C program? For example, I want to parse the contents of all
> > > > "*.dat "  files in a given directory. How can I do that?

> > > You can pass the filenames to the program as argv[1] through
> > > argv[n], then have the program open them.  With a unix-like
> > > shell, this is easy, you just type something like
> > > ./foobar *.dat

> >  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?
> > If so, how do I figure out which file ended where..!

> Don't top post, corrected.

> Under unix (and dos I think), if you invoke your program with

> ./foobar *.dat

> the shell will expand the *.dat to file1.dat file2.dat file3.dat etc...
> and in your code you can do

> #include <stdio.h>

> int mian(int argc,char *argv[]){

>    FILE file;

>    for(argv++;*argv!=NULL;argv++){
>       printf("processing %s\n",*argv);
>       if((file=fopen(argv,"r"))=NULL){

You could write this line as if(0){
:-)

ITYM == before NULL. Oh, and you misspelled main.

Jirka



Mon, 17 Nov 2003 01:20:53 GMT  
 Opening multiple files in C

Quote:





> > > >  Is it possible to open multiple files in a directory and read them one
> > by
> > > > one from a C program? For example, I want to parse the contents of all
> > > > "*.dat "  files in a given directory. How can I do that?

> > > You can pass the filenames to the program as argv[1] through
> > > argv[n], then have the program open them.  With a unix-like
> > > shell, this is easy, you just type something like
> > > ./foobar *.dat

> >  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat files?
> > If so, how do I figure out which file ended where..!

> Don't top post, corrected.

> Under unix (and dos I think), if you invoke your program with

> ./foobar *.dat

> the shell will expand the *.dat to file1.dat file2.dat file3.dat etc...
> and in your code you can do

> #include <stdio.h>

> int mian(int argc,char *argv[]){

int main

Quote:

>    FILE file;

>    for(argv++;*argv!=NULL;argv++){
>       printf("processing %s\n",*argv);
>       if((file=fopen(argv,"r"))=NULL){

                       *argv,"r"))==NULL

Quote:
>          fprintf(stderr,"can't open %s\n",argv);

                                            *argv

Quote:
>       }else{
>          /* do something */
>       }
>    }

>   return 0;
> }

> Tobias.

I hit the send button too quickly once again...
Tobias.


Mon, 17 Nov 2003 01:23:22 GMT  
 Opening multiple files in C

Quote:


> >       if((file=fopen(argv,"r"))=NULL){

> You could write this line as if(0){

That would omit possible side effects of fopen().
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz


Mon, 17 Nov 2003 01:21:31 GMT  
 Opening multiple files in C

Quote:


>> #include <stdio.h>

>> int mian(int argc,char *argv[]){

>int main

>>    FILE file;

FILE *file;

Quote:

>>    for(argv++;*argv!=NULL;argv++){

There still is the chance that argc == 0 and accessing argv[1] invokes
undefined behaviour.

Quote:
>>       printf("processing %s\n",*argv);
>>       if((file=fopen(argv,"r"))=NULL){

>                       *argv,"r"))==NULL

>>          fprintf(stderr,"can't open %s\n",argv);

>                                            *argv

>>       }else{
>>          /* do something */
>>       }
>>    }

>>   return 0;
>> }

Stefan


Mon, 17 Nov 2003 18:45:24 GMT  
 Opening multiple files in C
[...]

Quote:
> >       if((file=fopen(argv,"r"))=NULL){

> You could write this line as if(0){
> :-)

No. The former is a constraint violation while the latter is not.


Tue, 18 Nov 2003 01:46:35 GMT  
 Opening multiple files in C


Quote:

> >  But, when I say  fopen("*.dat", "r"),  would it open all the *.dat
files?

> No.  You fopen(argv[1], "r"), then fopen(argv[2], "r"), and so on.

> > If so, how do I figure out which file ended where..!

> What are you talking about?

[snip]

I think he wants to issue a single call and get back one stream/buffer
containing the concatenated contents of multiple files.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



Tue, 18 Nov 2003 06:49:59 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Multiple .cs files in assembly

3. Selecting multiple files in Win File->Open dialog

4. Q: File Open Dialogbox allow multiple file selection

5. Opening Multiple Files

6. opening multiple files in c

7. How to open multiple files??

8. VC6 Multiple Instances When Opening C Files

9. Multiple file opening

10. File Open Common Dialog multiple selection question

11. Opening multiple files using ShellExecute

12. opening multiple files

 

 
Powered by phpBB® Forum Software