Ok so this is really annoying me...I'm trying to go through and parse a bunch of .csv files for content but I can't get past trying to open them.
I want to read the directory contents and then open the specified file. I have the code working so it prints the directory contents successfully but when it tries to open the file it can only open the first 4 and then fails on the rest. If I hard code the filenames of the files it "FAILS" on (i.e. inFile.open("filename.txt")) it works just fine.
Any suggestions? Oh and I did try reading the entries from a list.txt but I can't get that to work either. My guess is the null character or something is interfering with the attempt to open it but I can't figure out how to work around that...I can do it in perl just not C++ :(
#include <iomanip>
#include <iostream>
#include <dirent.h>
#include <fstream>
#include <string>
using namespace std;
int main()
{
DIR *dirp;
struct dirent *entry;
int pos;
int linecount = 0;
if(dirp = opendir("C:\\csvfiles\\"))
{
while(entry = readdir(dirp))
{
//printf("%s\n", entry->d_name); //prints the files in the directory
string temp = entry->d_name;
if (temp.length() > 2) //skips over the . and .. entries
{
ifstream inFile; //creates a fstream object inFile
inFile.open(entry->d_name); //opens the file
if (!inFile.is_open()) //if file wasn't opened
{
cout << "Failed to open " << entry->d_name << endl;
// return 1;
}
else
{
cout << "Successfully opened " << entry->d_name << endl;
}
inFile.clear();
inFile.close();
} //end of while file exists
}
closedir(dirp);
}
return EXIT_SUCCESS;
}
This is my output...
C:\bom\Debug>bom.exe
Successfully opened 5A40 Combo.csv
Successfully opened 5A40 Head.csv
Successfully opened 5A80 Combo.csv
Successfully opened 5A80 Head.csv
Failed to open 5C1.csv
Failed to open 5C1A.csv
Failed to open 5C1x2.csv
Failed to open 5C3.csv
Failed to open 5C3P.csv
Failed to open 5C5.csv
Failed to open 5E3 Head.csv
Failed to open 5E3.csv