?
Solved

C++ - open files using wildcards

Posted on 1998-07-06
4
Medium Priority
?
960 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

765 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