?
Solved

Accessing all files in a directory

Posted on 2003-03-26
4
Medium Priority
?
211 Views
Last Modified: 2010-04-01
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
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
4 Comments
 
LVL 6

Accepted Solution

by:
GaryFx earned 80 total points
ID: 8214176
You're comparing the result of __findnext to 1, but it returns a 0 when it's successful.

Gary
0
 
LVL 7

Assisted Solution

by:billious
billious earned 80 total points
ID: 8216060
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9979102
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

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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

762 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