?
Solved

Accessing all files in a directory

Posted on 2003-03-25
32
Medium Priority
?
268 Views
Last Modified: 2010-04-01
I have the following code:

#include <fstream.h>
#include <iostream.h>
#include <string.h>
int main (int argc, char *argv[])
{
    argv[1] = "c:\\test2";
    argv[2] = "c:\\test2";
    char string[161];
    ifstream in(argv[1]);
         if(!in)                  
    {
         cout<<"File cannot be opened";            
         return 1;
    }
    ofstream out;
    out.open(argv[2]);        
    while(!in.eof())
    {
         in.getline(string,161);
         out << string << endl;
    }
    in.close();
    out.close();
       return 0;
}

This code is supposed to access all the files of *.txt (located in c:\\test2) and continue with the program. The program puts a carriage return after every 160 bits to make my .txt files look nice. However, I can only get this program to run and convert one .txt file at a time. I have ten files in my c:\test2 folder which I need all done at the same time or one after another. Can someone please help me or show me what I am missing. Thanks!
0
Comment
Question by:sorriv
[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
  • 13
  • 8
  • 7
  • +2
32 Comments
 
LVL 5

Expert Comment

by:cryptosid
ID: 8206877
heard about findfirst() and findnext() functions...these functions are present in DOS.H and they can be used to develop a simple loop which can give u the names of the files matching a specific pattern supplied to them...
here is how...

#include <dos.h>


void Search(char pattern[])
{
   //define a variable to hold the list of filenames...
   struct ffblk file;

   //specify the pattern to findfirst...
   if(findfirst(pattern,&file,FA_ARCH)==0)
   {
      //loop through the filenames till there are files // matching the pattern specified..
      do
      {
         printf("\nfile: %s",file.ff_fname);
         //here i have simply printed the names
         //you could open the files using the name and do whatever you want...got it...
      }while(findnext(&file)==0); //end of while loop
   }//end of if
}
//end of function

hope that helps
cryptosid
0
 

Expert Comment

by:gotenks
ID: 8207431
If you are using VC++ 6.0 :

#include <io.h> // for _findfirst and _findnext
...

int myFunction(char * in_file, char * out_file) {
   char string[161];
   ifstream in(in_file);

   if(!in)                  
   {
        cout<<"File cannot be opened";            
        return -1;
   }
   ofstream out;
   out.open(out_file);        
   while(!in.eof())
   {
        in.getline(string,161);
        out << string << endl;
   }
   in.close();
   out.close();

   return 0;
}

int search_dir(char * file_name) {
   struct _finddata_t c_file;
   long hFile;

   // if you want, you can use the value from  
   // file_name as a reference for your input
   // and output.
   // the code below perform search in it's,
   // where the .exe (the program) resides,
   // current directory.

   // Find first .txt file in current directory
   if( (hFile = _findfirst( "*.txt", &c_file )) == -1L ) {
      cout << "No *.txt files in current directory!\n";
      return -1;
   }
   else     {
      // get the file name and create a full path
      // you can use the "c_file.name" to get the file  
      // name.
      ...
      // call the function to manipulate the
      // current file.
      myFunction(in_file, out_file);
      ...
      // Find the rest of the .txt files
      while( _findnext( hFile, &c_file ) == 0 ) {
         // get the file name and create a full path
         // you can use the "c_file.name" to get the file  
         // name.
         ...
         // call the function to manipulate the
         // current file.
         myFunction(in_file, out_file);
         ...
      }
      _findclose( hFile );
   }
   return 0;
}

int main() {
   char file_name[161];

   strcpy(file_name, "c:\\test.txt");
   
   search_dir(file_name);
   return 0;
}

I have move your code inside the main() to another function myFunction(), so that you can perform the function of reading and writing inside the search_dir().

The above code will search for .txt file and manipulate the current .txt file and repeat the following process until no more .txt files found in the current directory.

If you are interested, you can also search for the .txt files in the subdirectories of the current directory too... but the current code couldn't accomplish that. need some modification to the code in search_dir().

For more information, refer to this link :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__find.2c_._wfind_functions.asp

Hope this will help too...

gotenks
0
 

Author Comment

by:sorriv
ID: 8207526
gotenks, I get the following error when I compile with your code.

error C2065: 'in_file' : undeclared identifier
error C2065: 'out_file' : undeclared identifier

Any suggestions?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sorriv
ID: 8207533
gotenks, I get the following error when I compile with your code.

error C2065: 'in_file' : undeclared identifier
error C2065: 'out_file' : undeclared identifier

Any suggestions?
0
 

Expert Comment

by:gotenks
ID: 8207551
of course, i didnt declare the in_file and out_file inside the search_dir()... you should declare it as :

char in_file[256]; // depend on what size you wished.
char out_file[256]; // depend on what size you wished.

and also you need to assign values to these 2 variables. before proceeding to call the myFunction().

example of in_file and out_file value would be :
in_file = "c:\\test\\file_name.txt";
out_file = "c:\\test\\file_name_out.txt";

hope this will help.
good luck.

gotenks
0
 

Author Comment

by:sorriv
ID: 8207592
gotenks, I get the following error when I compile with your code.

error C2065: 'in_file' : undeclared identifier
error C2065: 'out_file' : undeclared identifier

Any suggestions?
0
 

Author Comment

by:sorriv
ID: 8207614
gotenks, I get the following error when I compile with your code.

error C2065: 'in_file' : undeclared identifier
error C2065: 'out_file' : undeclared identifier

Any suggestions?
0
 

Author Comment

by:sorriv
ID: 8207738
gotenks..that helped a lot....now I am constantly getting an error that "File cannot be openedFile cannot be openedPress any key to continue"....There are two '.txt' files located in my 'c:\test' folder.....why else might I be getting this error? I have posted the revised code below if you would like to test yours.

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
  char string[161];
  ifstream in(in_file);

in_file = "c:\\test\\*.txt";
out_file = "c:\\test\\*.txt";

  if(!in)                  
  {
       cout<<"File cannot be opened";            
       return -1;
  }
  ofstream out;
  out.open(out_file);        
  while(!in.eof())
  {
       in.getline(string,161);
       out << string << endl;
  }
  in.close();
  out.close();

  return 0;
}

int search_dir(char * file_name) {
  struct _finddata_t c_file;
  long hFile;

  // if you want, you can use the value from  
  // file_name as a reference for your input
  // and output.
  // the code below perform search in it's,
  // where the .exe (the program) resides,
  // current directory.

  // Find first .txt file in current directory
  if( (hFile = _findfirst( "c:\\test\\*.txt", &c_file )) == -1L ) {
     cout << "No *.txt files in current directory!\n";
     return -1;
  }
  else     {
     // get the file name and create a full path
     // you can use the "c_file.name" to get the file  
     // name.
   
     // call the function to manipulate the
     // current file.
     myFunction(in_file, out_file);
     
     // Find the rest of the .txt files
     while( _findnext( hFile, &c_file ) == 0 ) {
        // get the file name and create a full path
        // you can use the "c_file.name" to get the file  
        // name.
       
        // call the function to manipulate the
        // current file.
        myFunction(in_file, out_file);
       
     }
     _findclose( hFile );
  }
  return 0;
}

int main() {
  char file_name[161];

  strcpy(file_name, "c:\\test\\*.txt");

    search_dir(file_name);
  return 0;
}



0
 

Expert Comment

by:gotenks
ID: 8207923
have make some changes to it, see if this help...

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
 char string[161];
 ifstream in(in_file);
 
 cout << "hello in " << in_file << endl;
 cout << "hello out " << out_file << endl;
//in_file = "c:\\test\\*.txt";
//out_file = "c:\\test\\*.txt";

 if(!in)                  
 {
      cout<<"File cannot be opened";            
      return -1;
 }
 ofstream out;
 out.open(out_file);        
 while(!in.eof())
 {
      in.getline(string,161);
      out << string << endl;
 }
 in.close();
 out.close();

 return 0;
}

int search_dir(char * file_path) {
 struct _finddata_t c_file;
 long hFile;

 char temp_path[256];
 char in_file[256];
 char out_file[256];

 // if you want, you can use the value from  
 // file_name as a reference for your input
 // and output.
 // the code below perform search in it's,
 // where the .exe (the program) resides,
 // current directory.

 strcpy(temp_path, file_path);
 strcat(temp_path, "*.txt");
 // Find first .txt file in current directory
 if( (hFile = _findfirst( temp_path, &c_file )) == -1L ) {
    cout << "No *.txt files in current directory!\n";
    return -1;
 }
 else     {
    // get the file name and create a full path
    // you can use the "c_file.name" to get the file  
    // name.
    strcpy(in_file, file_path);
    strcat(in_file, c_file.name);

    strcpy(out_file, file_path);
    strcat(out_file, c_file.name);
    // call the function to manipulate the
    // current file.
    myFunction(in_file, out_file);
   
    // Find the rest of the .txt files
    while( _findnext( hFile, &c_file ) == 0 ) {
       // get the file name and create a full path
       // you can use the "c_file.name" to get the file  
       // name.
       
       strcpy(in_file, file_path);
       strcat(in_file, c_file.name);

       strcpy(out_file, file_path);
       strcat(out_file, c_file.name);
       // call the function to manipulate the
       // current file.
       myFunction(in_file, out_file);
       
    }
    _findclose( hFile );
 }
 return 0;
}

int main() {
 char file_name[161];

 // just send the path/directory to search, instead of  
 // path and file name.
 strcpy(file_name, "e:\\practice\\test\\");

 search_dir(file_name);
 return 0;
}

I have also changed in the main(), the parameter that is sent to search_dir(). the code now send only the path to search for the .txt files... hope this helps... good luck.

gotenks
0
 

Expert Comment

by:gotenks
ID: 8208003
the error is because you pass in a wrong string of in_file and out_file

your value :
in_file = "c:\\test\\*.txt";
out_file = "c:\\test\\*.txt";

it should be :
in_file = "c:\\test\\ValidFileName.txt";
out_file = "c:\\test\\ValidFileName.txt";

when you assigned your value to the file input stream, in, it cannot find the "*.txt" file... that is the reason you get the error, i guess.
if your folder, c:\\test\\, have the following file :
   TextFile.txt
   OtherFile.txt
then you should write your in_file as :
in_file = "c:\\test\\TextFile.txt";

and
in_file = "c:\\test\\OtherFile.txt";


gotenks
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208089
You are already passing the values to in_file and out_file from another function. Then please don't modify it anywhere in myFunction () like you've done.

Mayank.
0
 

Author Comment

by:sorriv
ID: 8208149
OK....this code here seems to run fine without any errors and places a test file into the correct folder, however, it does not manipulate all of the *.txt files by placing a carriage return every 160 characters. It originally did this. So if I can just get the original to work with what we now have I will be set. THANK YOU SO MUCH FOR WHAT YOU HAVE HELPED ME WITH SO FAR!

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
char string[161];
ifstream in(in_file);


if(!in)                  
{
     cout<<"File cannot be opened";            
     return -1;
}
ofstream out;
out.open(out_file);        
while(!in.eof())
{
     in.getline(string,161);
     out << string << endl;
}
in.close();
out.close();

return 0;
}

int search_dir(char * file_path) {
struct _finddata_t c_file;
long hFile;

char temp_path[256];
char in_file[256];
char out_file[256];

// if you want, you can use the value from  
// file_name as a reference for your input
// and output.
// the code below perform search in it's,
// where the .exe (the program) resides,
// current directory.

strcpy(temp_path, file_path);
strcat(temp_path, "*.txt");
// Find first .txt file in current directory
if( (hFile = _findfirst( "c:\\test", &c_file )) == -1L ) {
   cout << "No *.txt files in current directory!\n";
   return -1;
}
else     {
   // get the file name and create a full path
   // you can use the "c_file.name" to get the file  
   // name.
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   strcat(out_file, c_file.name);
   // call the function to manipulate the
   // current file.
   myFunction(in_file, out_file);
   
   // Find the rest of the .txt files
   while( _findnext( hFile, &c_file ) == 0 ) {
      // get the file name and create a full path
      // you can use the "c_file.name" to get the file  
      // name.
     
      strcpy(in_file, file_path);
      strcat(in_file, c_file.name);

      strcpy(out_file, file_path);
      strcat(out_file, c_file.name);
      // call the function to manipulate the
      // current file.
      myFunction(in_file, out_file);
     
   }
   _findclose( hFile );
}
return 0;
}

int main() {
char file_name[161];

// just send the path/directory to search, instead of  
// path and file name.
strcpy(file_name, "c:\\test\\");

search_dir(file_name);
return 0;
}
0
 

Expert Comment

by:gotenks
ID: 8208340
not really sure if you can open the same file and perform the read and write at the same time...
however, if you want the file to be output to another file instead, try changing the out_file to another name. right now, you are using the same file name for input and output...

int search_dir(...) {
   ...
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   strcat(out_file, "out_"); // added
   strcat(out_file, c_file.name);
   ...
}

now the in_file will hold the value : c:\test\filename.txt
and the out_file will hold the value : c:\test\out_filename.txt

see if this is what you want...

and one more thing... why you have hard coded the filepath : _findfirst("c:\\test", &c_file)?
as in :

if( (hFile = _findfirst("c:\\test", &c_file )) == -1L ) {
  cout << "No *.txt files in current directory!\n";
  return -1;
}


gotenks
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208359
>> not really sure if you can open the same file and perform the read and write at the same time...

Don't think that you can. I guess Cobol is the only language through which it is achieved in some manner. You can read a record, modify it and then use the 'rewrite' statement and it puts the updated record back into the file.... no fusses about read mode/ write mode, etc.

Mayank.
0
 

Expert Comment

by:gotenks
ID: 8208521
so, from what mayank had mentioned, since we cannot open the same file for input and output at the same time, i guess the better solution is to output the result to another file.
you can also keep the output to a buffer (not so practical, if the size is big) or write it to a temp file and then copy the content of temp file into that same file after you have finished reading it. finally, delete the temp file.
i have tried using fstream instead of ifstream, and ofstream but the most i can get is to append it, using the "ios::in | ios::out | ios::app" mode :

fstream in_out("c:\\temp.txt", ios::in | ios::out | ios:: app);

original file content :
hello world
good bye

output content :
hello world
hello world
good bye
good bye


gotenks
0
 

Author Comment

by:sorriv
ID: 8210733
So I guess my ultimate goal.....looking in a directory (c:\test), opening all .txt files and placing carriage returns every 160 characters is not possible? If this is so maybe I should try a different language? What are your opinions on this?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210793
Why not? You can create the new files (using ofstream) in a different, temporary directory with the same file-name, then delete the files in the original directory and copy back the files from the temporary directory.

Mayank.
0
 

Author Comment

by:sorriv
ID: 8210861
Also, I can get the program to run with no errors and it revognizes the .txt file's in the folder, however, it erases the contents as opposed to placing a carriage return every 160 characters.My original code allowed for carriage returns. Is there any way to add this to the program now and get it all to flow?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210903
Did you try the approach that I suggested?
0
 

Author Comment

by:sorriv
ID: 8210991
mayankeagle, I did try that and Im not sure why it doesnt work?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8211024
Change:

>> strcpy(out_file, file_path);

to:

strcpy ( out_file, <some_other_path> ) ;

And then, repeat the entire process for copying back the files (means that everywhere, the file_path and <some_other_path> will be interchanged).

Mayank.

0
 

Author Comment

by:sorriv
ID: 8211160
This is what I have....the program runs and makes a new file "out_...." however it does not copy any data to the file and place the carriage returns. Any suggestions?



#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
char string[161];
ifstream in(in_file);


if(!in)                  
{
     cout<<"File cannot be opened";            
     return -1;
}
ofstream out;
out.open(out_file);        
while(!in.eof())
{
     in.getline(string,161);
     out << string << endl;
}
in.close();
out.close();

return 0;
}

int search_dir(char * file_path) {
struct _finddata_t c_file;
long hFile;

char temp_path[256];
char in_file[256];
char out_file[256];

strcpy(temp_path, file_path);
strcat(temp_path, "*.txt");
// Find first .txt file in current directory
if( (hFile = _findfirst( temp_path, &c_file )) == -1L ) {
   cout << "No *.txt files in current directory!\n";
   return -1;
}
else     {
   // get the file name and create a full path  
   // you can use the "c_file.name" to get the file  
   // name.
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   strcat(out_file, "out_try");
   strcat(out_file, c_file.name);
   
   myFunction(in_file, out_file);
   
   // Find the rest of the .txt files
   while( _findnext( hFile, &c_file ) == 0 ) {
       
     
      strcpy(in_file, file_path);
      strcat(in_file, c_file.name);

      strcpy(out_file, file_path);
      strcat(out_file, c_file.name);
       
      myFunction(in_file, out_file);
     
   }
   _findclose( hFile );
}
return 0;
}

int main() {
char file_name[161];

strcpy(file_name, "c:\\test\\");

search_dir(file_name);
return 0;
}

0
 

Author Comment

by:sorriv
ID: 8213492
I now am able to have it modify one single file in the directory....However, it does not go through all of them and do this. Any suggestions....code is below....If I can just get it to go through all the files in the director then I will be good to go.

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
char string[161];
ifstream in(in_file);


if(!in)                  
{
     cout<<"File cannot be opened";            
     return -1;
}


ofstream out;

out.open(out_file);        
while(!in.eof())
{
     in.getline(string,161);
     out << string << endl;
}
in.close();
out.close();

return 0;
}

int search_dir(char * file_path) {
struct _finddata_t c_file;
long hFile;

char temp_path[256];
char in_file[256];
char out_file[256];


strcpy(temp_path, file_path);
strcat(temp_path, "*.txt");
// Find first .txt file in current directory
if( (hFile = _findfirst( temp_path, &c_file )) == -1L ) {
   cout << "No *.txt files in current directory!\n";
   return -1;
}
else     {
   
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   
   strcat(out_file, c_file.name);
   strcat(out_file, "1");
   myFunction(in_file, out_file);
   
   // Find the rest of the .txt files
   while( _findnext( hFile, &c_file ) == 1 ) {

      myFunction(in_file, out_file);
     
   }
   _findclose( hFile );
}
return 0;
}

int main() {
char file_name[161];

strcpy(file_name, "c:\\test\\");

search_dir(file_name);
return 0;
}
0
 

Author Comment

by:sorriv
ID: 8213508
I am now able to get it to manipulate a single file the way I want it to. However, it will not go through all of the text files in the directory....any suggestions?

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
char string[161];
ifstream in(in_file);


if(!in)                  
{
     cout<<"File cannot be opened";            
     return -1;
}


ofstream out;

out.open(out_file);        
while(!in.eof())
{
     in.getline(string,161);
     out << string << endl;
}
in.close();
out.close();

return 0;
}

int search_dir(char * file_path) {
struct _finddata_t c_file;
long hFile;

char temp_path[256];
char in_file[256];
char out_file[256];


strcpy(temp_path, file_path);
strcat(temp_path, "*.txt");
// Find first .txt file in current directory
if( (hFile = _findfirst( temp_path, &c_file )) == -1L ) {
   cout << "No *.txt files in current directory!\n";
   return -1;
}
else     {
   
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   
   strcat(out_file, c_file.name);
   strcat(out_file, "1");
   myFunction(in_file, out_file);
   
   // Find the rest of the .txt files
   while( _findnext( hFile, &c_file ) == 1 ) {

      myFunction(in_file, out_file);
     
   }
   _findclose( hFile );
}
return 0;
}

int main() {
char file_name[161];

strcpy(file_name, "c:\\test\\");

search_dir(file_name);
return 0;
}
0
 

Author Comment

by:sorriv
ID: 8213778
I am now able to get it to manipulate a single file the way I want it to. However, it will not go through all of the text files in the directory....any suggestions?

#include <io.h> // for _findfirst and _findnext
#include <fstream.h>
#include <iostream.h>
#include <string.h>

char in_file[256];
char out_file[256];

int myFunction(char * in_file, char * out_file) {
char string[161];
ifstream in(in_file);


if(!in)                  
{
     cout<<"File cannot be opened";            
     return -1;
}


ofstream out;

out.open(out_file);        
while(!in.eof())
{
     in.getline(string,161);
     out << string << endl;
}
in.close();
out.close();

return 0;
}

int search_dir(char * file_path) {
struct _finddata_t c_file;
long hFile;

char temp_path[256];
char in_file[256];
char out_file[256];


strcpy(temp_path, file_path);
strcat(temp_path, "*.txt");
// Find first .txt file in current directory
if( (hFile = _findfirst( temp_path, &c_file )) == -1L ) {
   cout << "No *.txt files in current directory!\n";
   return -1;
}
else     {
   
   strcpy(in_file, file_path);
   strcat(in_file, c_file.name);

   strcpy(out_file, file_path);
   
   strcat(out_file, c_file.name);
   strcat(out_file, "1");
   myFunction(in_file, out_file);
   
   // Find the rest of the .txt files
   while( _findnext( hFile, &c_file ) == 1 ) {

      myFunction(in_file, out_file);
     
   }
   _findclose( hFile );
}
return 0;
}

int main() {
char file_name[161];

strcpy(file_name, "c:\\test\\");

search_dir(file_name);
return 0;
}
0
 

Expert Comment

by:gotenks
ID: 8214658
ok, here is more or less the code that will perform the task of copying the output to a temporary file and then re-copy the content of the temporary file back to the original output path/file.

// code start
int myFunction(char * in_file, char * out_file) {
   char string[161];
   ifstream in(in_file);

   if(!in) {
      cout<<"File cannot be opened";            
      return -1;
   }

   ofstream out;
   char temp_out_path[256];

   // set to whatever temporary path that you like
   strcpy(temp_out_path, "e:\\temp_output.txt");

   out.open(temp_out_path);        

   while(!in.eof()) {
      in.getline(string,161);
      out << string << endl;
   }
     
   in.close();
   out.close();

   // now copy back the content to the original output
   // path.
   in.open(temp_out_path);
   if ( !in ) {
      cout << "Temporary file cannot be opened";
      return -1;
   }

   out.open(out_file);
   if ( !out ) {
      cout << "File output cannot be opened";
      return -1;
   }

   while ( !in.eof() ) {
      in.getline(string, 161);
      out << string << endl;
   }

   in.close();
   out.close();

   // finally remove the temp file
   remove(temp_out_path);

   return 0;
}
// code end

that should complete your purposes. good luck.

gotenks
0
 

Accepted Solution

by:
gotenks earned 200 total points
ID: 8215371
int search_dir() {
   ...
   if((hFile = _findfirst(temp_path, &c_file)) == -1L) {
      cout << "No *.txt files in current directory!\n";
      return -1;
   }
   else {
      // first part.
      // this code before the while loop will copy
      // the file name for the FIRST .txt file
      // encountered.
      strcpy(in_file, file_path);
      strcat(in_file, c_file.name);

      strcpy(out_file, file_path);
      strcat(out_file, c_file.name);
      myFunction(in_file, out_file);
   
      // Find the REST of the .txt files
      // note that the c_file value that follows
      // is NOT the same as the above called by
      // _findfirst. _findnext will get the REST
      // of the .txt. therefore, you must realize
      // that c_file.name contains DIFFERENT value.
      while( _findnext( hFile, &c_file ) == 0 ) {
         // second part.
         // inside this while loop, the code
         // will get the file name for THE REST of
         // the .txt files encountered in the current
         // folder.
         // both the upper assignment (first part)
         // and inside this while loop (second part)
         // is NOT the same or contain DIFFERENT value.
         // therefore, you have to re-do the process
         // to get their respective file names.

         strcpy(in_file, file_path);
         strcat(in_file, c_file.name);

         strcpy(out_file, file_path);
         strcat(out_file, c_file.name);
         myFunction(in_file, out_file);
      }

      _findclose( hFile );
   }
   
   ...
}

please read the comment i have added above. just want to let you know what is actually going on with these _findfirst, and _findnext.
and the '...' inside the above code is NOT MEANT to be a code/syntax.

let say you have the following files in the (whatever folder)...

current folder :- c:\test\
.txt files :- myText.txt
              notes.txt
              anything.txt

_findfirst will return the file information of "myText.txt". Then "_findnext" will get the next file information, which happens to be "notes.txt". since you write the code or called "_findnext" inside the WHILE loop, therefore for EACH loop/search, "_findnext" will search the REST of the file inside the current folder, which is "c:\test\".

example :
_findfirst :- file name retrieved(c_file.name) : "myText.txt"
1st while loop for _findnext :- file name retrieved (c_file.name) : "notes.txt"
2nd while loop for _findnext :- file name retrieved (c_file.name) : "anything.txt"
3rd while loop for _findnext :- file name retrieved : [NONE]. therefore _findnext will return -1 upon failure to search for any OTHER .txt file and finally this -1 value will make the while loop to exit. therefore, exit your program.

hope the above will let you have a rough idea of what is actually going on. good luck.

gotenks
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8215655
Try this:


#include <io.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <stdio.h>

int myFunction ( char * in_file, char * out_file )
{
  char str[161] ;
  ifstream in ( in_file ) ;

  if ( ! in )                  
  {
    cout << "\n File cannot be opened. " ;            
    return -1 ;

  } // end if

  ofstream out ( out_file ) ;        

  while ( ! in.eof () )
  {
    in.getline ( str, 161 ) ;
    out << string << endl ;

  } // end while

  in.close () ;
  out.close () ;

  return 0 ;

} // end of myFunction ()

int search_dir ( char * file_path )
{
  struct _finddata_t c_file ;
  long hFile ;
  char temp_path[256] ;
  char in_file[256] ;
  char out_file[256] ;

  strcpy ( temp_path, file_path ) ;
  strcat ( temp_path, "*.txt" ) ;

  if ( ( hFile = _findfirst ( temp_path, &c_file ) ) == -1L )
  {
    cout << "\n No *.txt files in current directory! " ;
    return -1 ;

  } // end if
 
  else    
  {  
    strcpy ( in_file, file_path ) ;
    strcat ( in_file, c_file.name ) ;
    strcpy ( out_file, file_path ) ;  
    strcat ( out_file, c_file.name ) ;
    strcat ( out_file, "1" ) ;
    myFunction ( in_file, out_file ) ;
   
    while( _findnext ( hFile, &c_file ) == 1 )
    {
      strcpy ( in_file, file_path ) ;
      strcat ( in_file, c_file.name ) ;
      strcpy ( out_file, file_path ) ;  
      strcat ( out_file, c_file.name ) ;
      strcat ( out_file, "1" ) ;
      myFunction ( in_file, out_file ) ;    

    } // end while

    _findclose ( hFile ) ;
 
  } // end else

  return 0 ;

} // end of search_dir ()

int clean_up ( char * file_path )
{
  struct _finddata_t c_file ;
  long hFile ;
  char temp_path[256], in_file[256], temp_name[256] ;

  strcpy ( temp_path, file_path ) ;
  strcat ( temp_path, "*.txt" ) ;

  if ( ( hFile = _findfirst ( temp_path, &c_file ) ) == -1L )
  {
    cout << "\n No *.txt files in current directory! " ;
    return -1 ;

  } // end if
 
  else    
  {  
    strcpy ( in_file, file_path ) ;
    strcat ( in_file, c_file.name ) ;
    unlink ( in_file ) ;
   
    while( _findnext ( hFile, &c_file ) == 1 )
    {
      strcpy ( in_file, file_path ) ;
      strcat ( in_file, c_file.name ) ;
      unlink ( in_file ) ;

    } // end while

    _findclose ( hFile ) ;
 
  } // end else

  strcpy ( temp_path, file_path ) ;
  strcat ( temp_path, "*.txt1" ) ;

  if ( ( hFile = _findfirst ( temp_path, &c_file ) ) == -1L )
  {
    cout << "\n No *.txt1 files in current directory! " ;
    return -1 ;

  } // end if
 
  else    
  {  
    strcpy ( in_file, file_path ) ;
    strcat ( in_file, c_file.name ) ;
    strcpy ( temp_file, file_path ) ;
    strcat ( temp_file, c_file.name ) ;
    temp_file[strlen(temp_file)-1] = '\0' ;
    rename ( in_file, temp_file ) ;
   
    while( _findnext ( hFile, &c_file ) == 1 )
    {
      strcpy ( in_file, file_path ) ;
      strcat ( in_file, c_file.name ) ;
      strcpy ( temp_file, file_path ) ;
      strcat ( temp_file, c_file.name ) ;
      temp_file[strlen(temp_file)-1] = '\0' ;
      rename ( in_file, temp_file ) ;
 
    } // end while

    _findclose ( hFile ) ;
 
  } // end else

  return 0 ;

} // end of clean_up ()

int main ()
{
  char file_path[161] ;

  strcpy ( file_path, "c:\\test\\" ) ;
  search_dir ( file_path ) ;
  clean_up ( file_path ) ;
  return 0 ;

} // end of main ()


Hope that helps!

Mayank.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9558866
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to gotenks

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9578922
Split between gotenks and mayankeagle. Hope gotenks has no issues with that?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9587362
mayankeagle

My only argument with that is my directions were to give the points to the expert who is firstest iwth the mostest.

Having stated my rational I will leave it to the moderator.

-bcl
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 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