|
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
|
|
|
|
|
Thanks, now I don't get an error anymore.
However, it still doesn't work properly.
The program does nothing.
myfile.eof == false () never is wrong, apparently the file end is never reached.
I set a breakpoint and files contains 3 strings at runtime (this is correct).
Each of the files has 43 lines for a total of 129 lines;
I use
counter++;
cout << counter << ": "; Where counter is an int , starting a 0, to output the line numbers.
It passes 129 almost instantly and the while loop is infinte and line always is "" .
How come?
Why doesn't open () actually open the files or at least gives me some kind of error, why it won't work?
And more importantly, how does it work?
Thanks.
|
|
|
|
|
Have you tried something like:
while (myfile.good())
{
myfile.getline(line, sizeof(line));
wcout << line << 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
|
|
|
|
|
I just tries it changing line 's tpye to
WCHAR* line;
But now the pogram does nothing.
It doesn't even show the line numbers anymore.
I used breakpoints and looks like myfile.good () always returns false .
|
|
|
|
|
Megidolaon wrote: WCHAR* line;
Are you allocating memory for line ?
"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 just changed it to
WCHAR line[2000]; then to
WCHAR* line = new WCHAR; and finally to
WCHAR* line = new WCHAR[]; The program still does nothing in all cases.
|
|
|
|
|
Why not just leave it as:
wstring line; That way you don't have to be concerned with allocating too little or too much memory.
"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 had that first, but then the string remains empty and never gets filled.
No matter how I do it, the program doesn't ouput the content of the files.
|
|
|
|
|
Hello,
I have a CStatic on CMDIChildWnd but I am not able to change its background color.
I have tried using CWnd functions
CStatic* Static = new CStatic;
CDC* pDC = Static->GetDC();
pDC->SetBkMode(OPAQUE);
pDC->SetBkColor(RGB(150,150,255));
Pritha
|
|
|
|
|
Handle the WM_CTLCOLORSTATIC notification and return the brush to be used to paint the control.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|