C/c-prog

Advertisement


Question
hi sir my prog is

void main(){

   FILE *fp,*fw;
   char buffer[100];
   double filelatt,filelong,filechl;
  // double userlatt,userlong;
  // printf("\n Enter userlatt & userlong :");
  // scanf("%lf %lf",&userlatt,&userlong);

   fp = fopen("/home/cmmacs/seawifs/8-day/ascii/ascii_n7/1997/S19973611997365.L3m_8D_CHL_chlor_a_9km_chlorophyll.asc","r");
   fw = fopen("n71997.txt","a");

   if(fp==NULL){
       printf("\n No file");
       exit(0);
   }
   fgets(buffer,99,fp);

     while((fscanf(fp,"%lf %lf %lf",&filelatt,&filelong,&filechl))!=EOF){
       if((filelatt==19.208) && (filelong==67.208)){
           fprintf(fw,"%lf \n",filechl);

                                                             
my prob is i have so many files to input.. how to input 1 by 1 without entering the file name each n every time

Answer
Hello Swathi

You can have your program go through the directory and get the name of each file in the directory. Then, for each file name that matches your criteria, you can open the file. In that way you don't have to put in the name of each file at the keyboard. The way to do that depends on the operating system.

For windows, you can use the FindFirstFile and FindNextFile funtions. See These links for the Windows functions.
http://msdn.microsoft.com/en-us/library/aa364418%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa364428%28v=vs.85%29.aspx

For Linux/UNIX you would use the opendir/readdir/closedir functions
See these links fir Linux/UNIX
http://linux.die.net/man/3/opendir
http://linux.die.net/man/3/readdir
http://linux.die.net/man/3/closedir

Judging by your /home file location I'll bet you're using Linux. You can see a good UNIX/Linux sample program that lists the files in a directory at
http://www.metalshell.com/source_code/116/Read_Directory.html
The readdir function reads each entry in a directory. You will have to filter out the ones that match your requirements. You can use the fnmatch function to see if your file name matches some pattern.
See http://linux.die.net/man/3/fnmatch

I hope that helps you out. If you are using Windows, and need more help, let me know. I have not found a good sample for Windows, but I can write one for you if you need it.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.