So far, this has worked relatively nicely. However, I am having trouble in outputting to two separate files, and I'm hoping that you guys and gals can help me out. I'm reading in from a file, and outputting to two different files, based on what the input is. If anyone can advise me as to how to fix this, that would be wonderful. The strange part is that it successfully open/creates each file for the stream, and the writing works perfectly for the stream "preprocess" but not for "recordList". Issues in bold with comments. Thank you very much.
EDIT: I also have checked for recordList.fail() and its turning up false...I see no reason why this should not work!! :(
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string names;
string data;
string directory;
string namesDirectory;
string dataDirectory;
size_t found;
string currentFile=__FILE__;
string input;
int features=0;
int records=0;
char temp;
string dataTemp;
int debugCount=0;
names="raw-dataset.names";
data="raw-dataset.data";
found=currentFile.find_last_of("\\");
directory=currentFile.substr(0,found);
directory+="\\data\\";
namesDirectory=directory + names;
dataDirectory=directory + data;
cout << endl << "name file to be opened: " << endl << namesDirectory << endl;
cout << "data file to be opened: " << endl << dataDirectory << endl;
//Open the names file and count the number of features we have
ifstream openNames;
openNames.open(namesDirectory.c_str());
if(openNames.is_open())
{
cout << "names opened successfully. " << endl;
while(!openNames.eof())
{
getline(openNames,input);
if(input[0]!='|' && !input.empty())
{
features++;
}
}
features-=1;
openNames.close();
}
else
cout << "Unable to read file." << endl;
cout << "There are " << features << " features." <<endl;
//Open the data file to simply count the number of records
ifstream openData;
openData.open(namesDirectory.c_str());
if(openData.is_open())
{
cout << "data opened successfully for counting. " << endl;
while(!openData.eof())
{
openData.get(temp);
if(temp=='.')
records++;
}
openData.close();
}
else
cout << "Unable to read file for counting." << endl;
cout << "There are " << records << " records." << endl;
//Re-open data file to actually perform the tasks we need done
ofstream preprocess;
ofstream recordList;
ifstream openData2;
openData2.open(dataDirectory.c_str());
preprocess.open ("data//dataset.dat");
recordList.open ("data//answers.dat");
if(openData2.is_open() && preprocess.is_open() && recordList.is_open())
{
cout << "data files opened and created successfully for analysis. " << endl;
[B] preprocess << records << " " << features << "\n"; //Writes successfully
recordList << records << "\n";//Doesn't write successfully[/B]
while(!openData2.eof())
{
if(openData2.peek()=='n')
{
recordList << "nonad" << "\n";
getline(openData2,dataTemp,'.');
preprocess << "\n";
//This doesn't write successfully, but it goes to the correct spot in the code
}
else if(openData2.peek()=='a')
{
recordList << "ad" << "\n";
getline(openData2,dataTemp,'.');
preprocess << "\n";
//This doesn't write successfully, but it goes to the correct spot in the code
}
else
{
getline(openData2,dataTemp,',');
preprocess << dataTemp << " ";
//Works just fine
}
debugCount++;
cout << debugCount << " of " << records << endl;
}
openData2.close();
preprocess.close();
recordList.close();
}
else
cout << "Unable to read/write files for analysis." << endl;
return 0;
}