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.
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".
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".