Solved

C++ - open files using wildcards

Posted on 1998-07-06
4
838 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now