1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
void Program::LoadFile() {
string fileName = "";
ifstream inFile;
cin.clear();
cin.sync();
cout << "Enter a filename: ";
getline(cin, fileName);
if (fileName.empty()) {
cout << "Error: No filename given." << endl;
}
else {
inFile.open(fileName.c_str(), ios::in);
if (!inFile) {
cout << "Error: File not found." << endl;
return;
}
char *line1 = new char[512];
char *line2 = new char[512];
string fileKey = "#FILE#", line;
char c;
_employees.clear();
bool myBool = false;
getline(inFile, line);
for (int i=0; i<6; ++i) {
if (strcmp(fileKey.c_str(), line.c_str()) == 0)
myBool = true;
else
myBool = false;
}
int cnt = 0;
if(myBool) {
while (inFile >> noskipws >> c) {
line1[cnt] = c;
if (c == ':')
while (c != '\n') {
line2[cnt] = c;
++cnt;
}
else if (c == '\n') {
cnt = 0;
AddEmployee(line1, (double)atof(line2));
line1 = '\x0';
line2 = '\x0';
}
++cnt;
}
inFile.close();
cout << "Read " << cnt << " lines in file "
<< "and loaded the Employee List."
<< endl << endl;
}
else {
cout << "Sorry cannot load the file." << endl
<< "Wrong signature found." << endl;
inFile.close();
return;
}
}
}
|
So far that's what I've got and it doesn't work. Is there anyway to make it
work? What I've been trying to do for 10 hours now is... load a text file
like so:
#FILE#48027599
Some Name:<double> // double being a pay
Some Name:<double>
etc...
Then read first #FILE# (which will be my signature), the number
which will be held in a private variable for class. Finally,
the names looped in 1 string, the doubles in a double var. So
I can type them in my program like this:
Employee List #48027599
===========================================
John Doe: 32000
Jane Doe: 35000
===========================================
That's all. Please help me, oh and only C++ no C/C++... Thanks in advance.