Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for text files in a directory-- keeps finding jps too! 1

Status
Not open for further replies.

Shawnasie

Programmer
Aug 19, 2008
23
0
0
US
I am using Borland Builder C++. I have a dialog box which allows the user to select a file. Then I find the directory for that file, and use that as a path to search for all text files in that folder. My trouble is that if there are any jpeg files in that folder, this method seems to find those images also. When I remove the jpeg files, there is no trouble. Unfortunately, I need to access those images later on, so I need them to stay in the folder.

I don't understand why this is happening since in my code I specify that the search involves "*.txt". Please help me! I am very new to Borland, and not that confident in C++ in general.

Code:
#include <iostream>
#include <stdio.h>
#include <fstream>

void __fastcall TForm1::Button1Click(TObject *Sender)
{


   if( OpenDialog1->Execute() == True ){


       	AnsiString f = OpenDialog1->FileName; //chosen file
       	char * slash = "\\";
	        int i;

    		while(!AnsiSameCaption(slash,f.AnsiLastChar())){

       		i = f.Length();
       	 	f.Delete(i,1);

    		}

    		AnsiString fileType ("*.txt");
    		AnsiString path = f + fileType;
   	 	WIN32_FIND_DATA fd;
        	HANDLE hFindTxt = FindFirstFile(path.c_str(),&fd);


        	char t[3][20];

        	if (hFindTxt != INVALID_HANDLE_VALUE) {
        		do{

            			if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){

        				ifstream fin(fd.cFileName);//Opens each file and reads in the appropriate data
       					fin >> t[0] >> t[1] >> t[2];
        				fin.close();

        			}

         		}while (FindNextFile(hFindTxt,&fd));

    		}else ShowMessage("Cannot find text files");


    		FindClose(hFindTxt);
	}
}

So for example the path might be something like "C:\Documents and Settings\Lab1\My Documents\7-28-2008\*.txt". And then it will find a file named "C:\Documents and Settings\Lab1\My Documents\7-28-2008\IMG_0477.jpg".
 
hi,
i've tried your program and its working fine..i've tried counting the number of text files the program finds in the directory and it returns the correct number even if there are jpg files.
perhaps, you could try this for your while statement:
while (FindNextFile(hFindTxt,&fd) != 0)
 
Thank you, warinski. I have tried it again, and it works the way I wanted it to, so I am not sure what happened. I am sorry to have taken up your time on that. I must have changed something and didn't realize it. I do have another question though, if you don't mind. I am doing the exact same thing, only now I want to find the jpg files, and then show them on my gui.

However, the compiler is having trouble with the highlighted line of code (see below). It has the error "First chance exception at $7C812AEB. Exception class EIinvalidGraphic with message 'Unknown picture file extension (.JPG)'. "

Code:
		AnsiString fileType2 ("*jpg");
		AnsiString path2 = f + fileType2;
		WIN32_FIND_DATA fd2;
		HANDLE hFindJpg = FindFirstFile(path2.c_str(),&fd2);
	if (hFindJpg != INVALID_HANDLE_VALUE) {
			do{

				if(!(fd2.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
					AnsiString image = AnsiString(fd2.cFileName);
					[COLOR=red]Image1->Picture->LoadFromFile(image);[/red]

				}

				}while(FindNextFile(hFindJpg,&fd2));
			}else ShowMessage ("Cannot find images.");

		FindClose(hFindJpg);
 
Did you include jpeg.hpp?


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I just realized that first line of code I just posted should actually read:

Code:
AnsiString fileType2 ("*.jpg");

And the part that I tried to highlight, i.e. the part that has trouble is:

Code:
Image1->Picture->LoadFromFile(image);

Now, I said the compiler had trouble, but actually this builds just fine, it's in run time that there is a problem.
 
Thank you, 2ffat. I included that file and it totally works now!!
 
Occam's Razor, sometimes it is the simplest answer. ;-)


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top