Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ - open files using wildcards

Posted on 1998-07-06
4
Medium Priority
?
1,025 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

Technology Partners: 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

610 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