• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Accessing all files in a directory

I have the following code. It manipulates only one file in the directory I assign it to do. I need it to manipulate all files until there are no more. Where in this code am I messing up?

#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
sorriv
Asked:
sorriv
2 Solutions
 
GaryFxCommented:
You're comparing the result of __findnext to 1, but it returns a 0 when it's successful.

Gary
0
 
billiousCommented:
After your findnext, you don't appear to be changing the values of in_file & out_file, even if the return from _findnext is correct.

...Bill
0
 
bcladdCommented:
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 split between GaryFX and billious

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now