Solved

C++ - open files using wildcards

Posted on 1998-07-06
4
932 Views
Last Modified: 2012-08-14
I need a simple routine which will open any file in a directory matching my criteria (like *.txt) until directory has been searched. Once a file is found I will do some processing of it then move on to the next one. I only know how to open a file using fstream::open, but haven't used a wildcard to open multiple files.
0
Comment
Question by:englm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
trestan earned 50 total points
ID: 1167262
I have compiled a similiar program before.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167263


#include <stdio.h>
#include <dir.h>
#include <conio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>

int main(void)
{
   void readfile(FILE *fp);

   struct ffblk ffblk;
   struct ftime new_dt,old_dt;
   FILE *new_fp,*old_fp;
   int done,new_handle,old_handle;
   char filename[12],buffer[MAXPATH];

   chdir ("C:/files");        //change directory to which contain datafile
   done = findfirst("*.hpg",&ffblk,0);        //find file "*.hpg"
   strcpy(filename , ffblk.ff_name);            //copy a string
   printf("%s",filename);
   new_fp = fopen(filename,"r");          //open found file

   new_handle = fileno(new_fp);
   getftime(new_handle,&new_dt);          //get date and time of the file
   printf("Date:%02u",new_dt.ft_month);
   getch();

   while(!done)
   {
      printf("%s\n", ffblk.ff_name);
      done = findnext(&ffblk);            //find next file

      strcpy(filename , ffblk.ff_name);            //copy string
         printf("%s",filename);
         old_fp = fopen(filename,"r");          //opne file found

         old_handle = fileno(old_fp);
         getftime(old_handle,&old_dt);          //get date and time of the file
         printf("Date:%02u",old_dt.ft_year);
         getch();

 //Do your search in the file.
     
      new_handle = fileno(new_fp);
         getftime(new_handle,&new_dt);
      printf("minutes:%02u",new_dt.ft_min);
      fclose (old_fp);
      getch();
   }

   readfile(new_fp);
   getch();
   chdir (buffer);
   return 0;
}

//The following is an example to search a char in the file.

void readfile(FILE *fp)

{
char ch;
float U,SHGC;

//fp=fopen("P2aw.hpg","r");
ch=fgetc(fp);
while(ch!=EOF)
      {
   if(ch=='U')
         {
      fscanf(fp," %c %f",&ch,&U);
      }
   if(ch=='S')
         {
      ch=fgetc(fp);
      if(ch=='H')
                  {
         ch=fgetc(fp);
         if(ch=='G')
               {
            ch=fgetc(fp);
            }
         fscanf(fp," %c %f",&ch,&SHGC);
         }
      }
   ch=fgetc(fp);
   }
printf("U=%f SHGC=%f\n",U,SHGC);
fclose(fp);
fp=fopen("DATAU","w");
fprintf(fp,"%f",U);
fclose(fp);
fp=fopen("DATAS","w");
fprintf(fp,"%f",SHGC);
fclose(fp);
}

0
 
LVL 11

Expert Comment

by:alexo
ID: 1167264
trestan gave a DOS solution.
For Windows use FindFirstFile(), FindNextFile() and FindClose() APIs.
0
 

Expert Comment

by:hlava
ID: 1167265
I'm Using Builder C++ WIN

When i want open some file i use OPENDIALOG->Execute()
and OPENDIALOG->FileName is the users file
in this OPENDIALOG could set the filters as *.txt,*.dwg ....
all what i want
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question