The failbit flag is being set and I'm not sure why?

I'm writing a program for class, and the failbit flag is being set on ordinary text files that are not empty. The actual assignment is to read files out of a directory in alphabetical order.
Here's the code:

#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <dirent.h>
#include <sys/types.h>

using namespace std;

vector <string> sortDirectory( const string& path = string() )
{
string allFiles = "";
vector <string> result;
dirent* de;
DIR* dp;
int errno = 0;
dp = opendir( path.empty() ? "." : path.c_str() );
if (dp)
{
while (true)
{
errno = 0;
de = readdir( dp );
if (de == NULL ) break;
result.push_back(string(de->d_name));
}
closedir(dp);
sort(result.begin(), result.end());
}
return result;
}

string getTxt(string fileName)
{
//convert string to char array
char * cstrFile = new char [fileName.size() + 1];
string txt = "";
string tempLine = "";
strcpy (cstrFile, fileName.c_str());
//cout << cstrFile;
ifstream readTxt;
readTxt.open(cstrFile);
//if the filename is not more than 3 characters long it cannot be a .txtfile
if(readTxt.bad())
{
cout << "bad" << endl;
}
if(!readTxt.fail() && fileName.size() > 2)
{
while(!readTxt.eof())
{
getline(readTxt, tempLine);
//cout << tempLine << endl;
txt += tempLine;

}
}
else
{
cout << "fail" << endl;
}
cout << txt;
readTxt.close();
return txt;
}

int main()
{
vector <string> files = sortDirectory("./tests");
for(int count = 0; count < files.size(); count++)
{
cout << files[count] << endl;
cout << getTxt(files[count]) << endl;
}
}

The output looks like this:
.
fail

..
fail

.HeliGame.cpp.swp
fail

Helicopter Game.C~
fail

copelande_hw2.txt
fail

gettysburg.txt
fail

readFilesLexi1st.cpp
#include <algorithm>#include <string>#include <cstring>#include <vector>#include <iostream>#include <fstream>#include <dirent.h>#include <sys/types.h>using namespace std;vector <string> sortDirectory( const string& path = string() ) {string allFiles = ""; vector <string> result; dirent* de; DIR* dp; int errno = 0; dp = opendir( path.empty() ? "." : path.c_str() ); if (dp){while (true) { errno = 0; de = readdir( dp ); if (de == NULL ) break; result.push_back(string(de->d_name)); } closedir(dp); sort(result.begin(), result.end()); }return result;}string getTxt(string fileName){ //convert string to char array char * cstrFile = new char [fileName.size() + 1]; string txt = ""; string tempLine = ""; strcpy (cstrFile, fileName.c_str()); //cout << cstrFile; ifstream readTxt; readTxt.open(cstrFile); //if the filename is not more than 3 characters long it cannot be a .txtfile if(readTxt.bad()) { cout << "bad" << endl; } if(!readTxt.fail() && fileName.size() > 2) {while(!readTxt.eof()) { getline(readTxt, tempLine); //cout << tempLine << endl; txt += tempLine; } } else { cout << "fail" << endl; } cout << txt; readTxt.close(); return txt;}int main(){vector <string> files = sortDirectory("./tests");for(int count = 0; count < files.size(); count++){ cout << files[count] << endl; cout << getTxt(files[count]) << endl;}}#include <algorithm>#include <string>#include <cstring>#include <vector>#include <iostream>#include <fstream>#include <dirent.h>#include <sys/types.h>using namespace std;vector <string> sortDirectory( const string& path = string() ) { string allFiles = ""; vector <string> result;dirent* de; DIR* dp; int errno = 0; dp = opendir( path.empty() ? "." : path.c_str() ); if (dp) { while (true) {errno = 0; de = readdir( dp ); if (de == NULL ) break; result.push_back(string(de->d_name)); }closedir(dp); sort(result.begin(), result.end()); }return result; }string getTxt(string fileName){ //convert string to char array char * cstrFile = new char [fileName.size() + 1]; string txt = "";string tempLine = ""; strcpy (cstrFile, fileName.c_str()); //cout << cstrFile; ifstream readTxt; readTxt.open(cstrFile); //if the filename is not more than 3 characters long it cannot be a .txtfile if(readTxt.bad()) { cout << "bad" << endl; } if(!readTxt.fail() && fileName.size() > 2) { while(!readTxt.eof()) { getline(readTxt, tempLine); //cout << tempLine << endl; txt += tempLine; } } else { cout << "fail" << endl; } cout << txt; readTxt.close(); return txt;}int main(){vector <string> files = sortDirectory("./tests");for(int count = 0; count < files.size(); count++){ cout << files[count] << endl; cout << getTxt(files[count]) << endl;}}

My question is why is it failing to read on everything but the code itself?
This is homework so a hint is all I'm asking?
First post, excited to be a new member, sorry if it's much too long.
Pls use the code tags
You read the file names from the ./tests directory but you don't specify the directory when opening the files with ifstream.
Last edited on
Thanks for the help, it took a little fiddling but it works. Thank you again peter.
I will do code tags next time
Topic archived. No new replies allowed.