Click to See Complete Forum and Search --> : I guess this should be in the programming forum
nothing
09-06-2004, 03:00 AM
I wrote a C++ program that will get all the files inside a folder and write them to a .txt file. The first file file is always two dots (the "up one level" thing). I was wondering how to make my program not to print that on the .txt file. This is what I've tried already:
if(filename != "..")
// print file name to .txt file
Thanks!
fishybawb
09-06-2004, 08:37 AM
Seeing as the first file is always "..", just try missing out the first file ;) I'm not sure how you're reading in the filenames, but if they're retrieved in the order they appear in the folder (.. first), then just don't output the first filename to the text file.
nothing
09-06-2004, 11:35 AM
Here's the code:
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
int main()
{
std::string filename;
std::string pagename;
std::string directory;
std::cout << "Type in a name for the .html file: ";
std::getline(std::cin, filename);
std::cout << "\nType in the name that is going to\n"
"appear at the top of your web browser: ";
std::getline(std::cin, pagename);
std::cout << "\nWhere are the files? ";
std::getline(std::cin, directory);
filename += ".html";
directory += "\\*";
std::ofstream html(filename.c_str());
html << "<html><head><title>" << pagename << "</title></head>\n\n<body>\n\n";
WIN32_FIND_DATA file;
HANDLE find_handle = INVALID_HANDLE_VALUE;
DWORD the_error;
find_handle = FindFirstFile(directory.c_str(), &file);
if(find_handle == INVALID_HANDLE_VALUE){
std::cout << "\nDirectory not found.\n";
std::cin.get();
return -1;
}
else{
while(FindNextFile(find_handle, &file) != 0){
html << "<a href=\"" << file.cFileName
<< "\">" << file.cFileName << "</a><br>\n";
}
}
the_error = GetLastError();
if(the_error == ERROR_NO_MORE_FILES){
FindClose(find_handle);
}
else{
std::cout << "\nError finding files.\n";
std::cin.get();
return -1;
}
std::cout << '\n' << filename << " created successfully!\n";
std::cin.sync();
std::cin.get();
return 0;
}
How can I not display the first file? a continue maybe? Thanks for the ideas.
fishybawb
09-06-2004, 11:57 AM
Add a find file thingy that doesn't actually use the file it finds on the first time through the loop:
// skip the first file
FindNextFile(find_handle, &file);
while(FindNextFile(find_handle, &file) != 0)
{
html << "<a href=\"" << file.cFileName
<< "\">" << file.cFileName
<< "</a><br>\n";
}
The "no files found" code doesn't seem to work though.
ScaryBinary
09-06-2004, 12:44 PM
My file management skills in C/C++ are practically non-existent, but I'm taking a shot at this anyway!
The WIN32_FIND_DATA structure has a member called dwFileAttributes. If this is equal to FILE_ATTRIBUTE_DIRECTORY, then the found item is a directory. Maybe you could use this - look at it each time you get a result, and if the item is a directory, skip it.
if (file.dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY ) {
// This is a directory ...
}
(not sure if that should be a "&&" or just an "&"...)
I'm assuming here, of course, that you only want to add files to your HTML file, and not directories. Maybe that's a bad assumption.
....or can't you just do something inside your while loop like:
else{
while(FindNextFile(find_handle, &file) != 0){
if( file.cFileName != ".." ) {
html << "<a href=\"" << file.cFileName
<< "\">" << file.cFileName << "</a><br>\n";
}
}
}
nothing
09-06-2004, 02:17 PM
I got it working like this:
else{
while(FindNextFile(find_handle, &file) != 0){
if (file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
continue;
else{
html << "<a href=\"" << file.cFileName
<< "\">" << file.cFileName << "</a><br>\n";
}
}
}
Thanks both of you :D
ScaryBinary
09-06-2004, 03:12 PM
A word of caution: that if statement has a great chance to cause you much heartache later on. That dwFileAttributes can be one or a combination of values. Check out the definition of WIN32_FIND_DATA (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/win32_find_data_str.asp).
The point is, if an item is a directory, and it has some other attribute like Read-Only or Archive set, your if statement will fail because dwFileAttributes will be equal to FILE_ATTRIBUTE_DIRECTORY + FILE_ATTRIBUTE_ARCHIVE, for instance, not just FILE_ATTRIBUTE_DIRECTORY. That's why my sample code attempted to "AND" (or mask) out the FILE_ATTRIBUTE_DIRECTORY instead of checking for the actual value. I think something like the following would be better
while(FindNextFile(find_handle, &file) != 0){
/* If the directory attribute isn't set, add the filename. */
if ( ! (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
html << "<a href=\"" << file.cFileName
<< "\">" << file.cFileName << "</a><br>\n";
}
}
You can also avoid that "continue" statement...! You'll have to verify my logic. I tend to get confused with ANDs and NOTs....:rolleyes:
the win32 api makes me want to be sick all over my keyboard
*puke*
im making a load of OO wrappers for the win32 api .. its giving me sleepless nights trying to program with it ;)
fishybawb
09-20-2004, 05:05 AM
Originally posted by cwin
im making a load of OO wrappers for the win32 api .. its giving me sleepless nights trying to program with it ;)
Save yourself some time, it's called MFC ;) Although I start to get a bit nauseous using that too :D
ScaryBinary
09-20-2004, 03:17 PM
"MFC" is a four-letter word!
DocEvi1
09-20-2004, 06:37 PM
Psst,
dir *.* /b /a >c:\list.txt
Originally posted by fishybawb
Save yourself some time, it's called MFC ;) Although I start to get a bit nauseous using that too :D Yes it sucks :D
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.