|
- Please check the posted code (for instance use
< to escape the symbol < when required). - Why are you calling directly the timer function inside
OnInitDialog (instead of starting the timer? You may actually do it, but usually you haven't...)? - Since you're calling directly (form
OnInitDialog ) your timer function (OnBnClickedTimer ) it doesn't make sense the OnStopTimer call in the latter.
Anyway your application 'freezes' possibly because of a bug in your loop. You should debug it in order to have a clue about (ispecting your code is also difficult because of the, already mentioned, bad formatting).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Also i think he is not passing correct id to KillTimer..
Regards,
Sandip.
|
|
|
|
|
Yes, you are right, of course.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Timers tutorial[^] is for you.
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
I want to open all files with a certain name and/or a certain type, but for that I need to know the contents of a directory first.
How do I see them?
Thanks.
|
|
|
|
|
FindFirstFile and FindNextFile should do it.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
|
Thanks.
I got something (I think) with FindFirstFile and FindNextFile .
Though, it doesn't quite work, this whole code is in a try catch block, but still throws a System.AccessViolationException (which is .NET specific and I can't use it or acces it) and does nothing:
try
{
fstream file;
vector<lpwin32_find_dataw> files;
LPCTSTR namepointer;
HANDLE handle;
string line = "";
int i = 0;
int size;
char directory [] = "C:\\Folder";
char *stringpointer = directory ;
files.assign(1, LPWIN32_FIND_DATAW());
handle = FindFirstFile(namepointer, files[i]);
while (handle != INVALID_HANDLE_VALUE)
{
FindNextFile(handle, files[i]);
files.assign(1, LPWIN32_FIND_DATAW());
i++;
}
size = files.size();
cout << files.size() << endl;
for (int k = 0; k < size; k++)
{
cout << "Reading File:" << files[k]->cFileName << endl;
}
getchar();
return 0;
}
catch (exception ex)
{
cout << "An error has occurred." << endl;
cout << ex.what() << endl;
}</lpwin32_find_dataw> I want to read the filenames to check if they're txt files with the right kind of name and then read from them to process the text of each.
Thanks.
|
|
|
|
|
Megidolaon wrote: ...but still throws a System.AccessViolationException...
Where/when?
Why do you have a vector of WIN32_FIND_DATA structures? One is sufficient.
Megidolaon wrote: handle = FindFirstFile(namepointer, files[i]);
What is the value of namepointer ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
It throws the exception right at the start and does nothing else.
DavidCrow wrote: Why do you have a vector of WIN32_FIND_DATA structures? One is sufficient.
FindFirstFile and FindNextFile seemed to return only as single file, so I assumed LPWIN32_FIND_DATAW represented a single file.
How does that work anyway?
The struct only has a few attributes that seem like file attributes, which wouldn't make much sense in a collection (like filename or creation date).
Originally I had
namepointer = stringpointer; but that won't do.
I get an error that char* cannot be converted to LPCTSTR.
I must have deleted the line and didn't notice.
Though, it was done like that in the article Hamid posted (then again, I got 10 errors from that code).
|
|
|
|
|
Megidolaon wrote: FindFirstFile and FindNextFile seemed to return only as single file, so I assumed LPWIN32_FIND_DATAW represented a single file.
It does, hence the need for a loop.
Look through this snippet to see where you went awry:
vector<string> files;
WIN32_FIND_DATA findData;
BOOL bFound = TRUE;
HANDLE handle = FindFirstFile("C:\\*.*", &findData);
if (handle != INVALID_HANDLE_VALUE)
{
while (bFound)
{
files.push_back(findData.cFileName);
bFound = FindNextFile(handle, &findData);
}
}
for (int k = 0; k < files.size(); k++)
cout << "Reading File:" << files[k].c_str() << endl;
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thanks, though this code stil throws 2 errors.
First one that I can't convert from const char[7] to LPCWSTR (in FindFirstFile ).
Second one is that I can't convert from WCHR[260] to std::string & (files.push_back(findData.cFileName); ).
|
|
|
|
|
Megidolaon wrote: Thanks, though this code stil throws 2 errors.
That's because you are using Unicode. Change string to wstring and prepend the string literal with an L .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I see now it compiles.
But the program doesn't quite work.
It doesn't display anything and when setting a breakpoint it takes over a minute until it reaches it (first call of FindFirstFile )
After that line I get an error, saying that there is no cource code available for the current position.
I don't get any error or output if I don't use a breakpoint.
|
|
|
|
|
Megidolaon wrote: It doesn't display anything...
Does the files vector contain anything?
What does your code look like?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
vector<wstring> files;
WIN32_FIND_DATA findData;
BOOL bFound = TRUE;
HANDLE handle = FindFirstFile(L"C:\Dokumente und Einstellungen\Student\Eigene Dateien", &findData);
if (handle != INVALID_HANDLE_VALUE)
{
while (bFound)
{
files.push_back(findData.cFileName);
bFound = FindNextFile(handle, &findData);
}
}
for (int k = 0; k < files.size(); k++)
{
cout << "Reading File:" << files[k].c_str() << endl;
}
getchar();</wstring> I copied your code and used wstring, then added an L to the filepath, as you said.
I used getchar() , to keep the console open.
I made a break point at the line of files.push_back(findData.cFileName);, but the program never reaches that line.
It simply does nothing, probably because when I put a breakpoint at the FindFirstFile line and then go the next line with F11, I get an error telling me that for the current position no source code is avaiable.
modified on Monday, March 2, 2009 5:23 AM
|
|
|
|
|
Megidolaon wrote: HANDLE handle = FindFirstFile(L"C:\Dokumente und Einstellungen\Student\Eigene Dateien", &findData);
You need to use double backslashes.
What is the value of handle ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I have double backslashes, codeproject didn't display the code properly.
I tried it with this conde and with using \\*.* at the ne dof the filepath, but it's the same.
I get that error in both cases.
|
|
|
|
|
Megidolaon wrote: I have double backslashes, codeproject didn't display the code properly.
CodeProject does not care if, or how many, backslashes are used.
Megidolaon wrote: I get that error in both cases.
What error?
Have you checked the return value of FindFirstFile() ? If it is INVALID_HANDLE_VALUE , have you called GetLastError() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I cannot check the return value of FindFirstFile because if I set a breakpoint, I get the error that the code is unavalable for the current position at the line of the FindFirstFile function.
|
|
|
|
|
Megidolaon wrote: I cannot check the return value of FindFirstFile...
What's keeping you from doing this:
vector<string> files;
WIN32_FIND_DATA findData;
BOOL bFound = TRUE;
HANDLE handle = FindFirstFile(L"C:\\Dokumente und Einstellungen\\Student\\Eigene Dateien\\*.*", &findData);
if (handle != INVALID_HANDLE_VALUE)
{
while (bFound)
{
files.push_back(findData.cFileName);
bFound = FindNextFile(handle, &findData);
}
for (int k = 0; k < files.size(); k++)
cout << "Reading File:" << files[k].c_str() << endl;
}
else
cout << GetLastError() << endl;;
getchar();
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I use this, but I get this error when trying to read the names from the vector.
error C2664: 'void std::basic_ofstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)': Covertion of Parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const wchar_t *' not possible
I use wofstream to open the files, but I get that error when trying to read the names from the vector, yet I don't get an error when using a const filename (a random string literal).
Also I get this error multiple times from getline():
error C2784: "std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)": template-Argument for "std::basic_istream<_Elem,_Traits> &" could not be deducted from "std::wofstream"
It's the same error regardless if line is a string or a wstring .
|
|
|
|
|
This is a different problem altogether. What does your code to open and read from a file look like?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
This is my code:
vector<wstring> files;
WIN32_FIND_DATA findData;
BOOL bFound = TRUE;
HANDLE handle;
string name ="";
wofstream myfile;
wstring line;
handle = FindFirstFile(L"C:\\Dokumente und Einstellungen\\Student\\Eigene Dateien\\*.mp", &findData);
if (handle != INVALID_HANDLE_VALUE)
{
while (bFound)
{
files.push_back(findData.cFileName);
bFound = FindNextFile(handle, &findData);
}
}
else
{
cout << GetLastError() << endl;
}
for (int k = 0; k < files.size(); k++)
{
myfile.open(files.at(k));
while (myfile.eof() == false)
{
getline(myfile, line);
wcout << line << endl;
}
myfile.close();
}</wstring> It's pretty much the same as you posted, except that I use wofstream instead of ofstream and wsring for line instead of string.
And I use wcout instead of cout .
.mp files are just txt files with a different extension.
|
|
|
|
|
Megidolaon wrote: wofstream myfile;
This should be an "input" file since you are reading from it.
Megidolaon wrote: myfile.open(files.at(k));
at() returns a string , not a const char * . Hint: use the c_str() method.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|