Solved

C++ - open files using wildcards

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 65
Would like to move button in a function 3 79
Unresolved External Symbols 3 105
Error C2678: binary '!=': no operator found... 4 58
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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