Re: Parsing help
"Sammo" <msammart@wnec.edu> wrote in message
news:1150740365.827887.96040@c74g2000cwc.googlegroups.com...
Hey, sorry about the messy format let me know if this is any better.
My question is how do i get it so the user is able to input a new
salary so that it will replace the old on within the text file. As for
the (choice == 2) that comes from a menu where the user is able to pick
1 of 2 choices. The menu works fine, i'm just having a trouble
implementing this last option. Also i realize there is 6 brackets at
the end. Some of them aren't for the snippet i posted, they go with
the rest of the program.
Here, I've formatted it a bit better. A few comments in the code marked
with //
if (choice == 2)
{
cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
// EmpID should be std::string
cin >> EmpID;
cout << " " << endl;
// Buffer shoudl be
char buffer[20]; // What is buffer for? Your're not using it.
{ // Why is this here? No reason to block the following code
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20]; // Again, you're not using this var
char backupmatrix[100][100];
// Better here would be std::vector< std::string > backupmatrix;
ifstream employeefile ("employee.txt");
if (! employeefile.is_open())
{
cout << "Error opening file";
exit (1); // Change this to return instead of exit.
}
int linecount = 0;
int IDlength = strlen(EmpID);
while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100);
// using std::vector< std::string > I would use:
// std::string Line;
// employeefile.getline( Line );
// backupmatrix.push_back( Line );
cout << backupmatrix[linecount] << endl;
// That line would work without changes.
// with std::string you can use string.compare()
for( int counter = 0; counter < IDlength; counter++)
{
if ( backupmatrix[linecount][counter+2] != EmpID[counter] )
{
break;
}
if ( counter == (IDlength-1) )
{
cout << "TEST!!!!!!!!!" << endl;
} //for debugging purposes
line "backupmatrix[linecount]"
}
linecount++;
}
}
}
Okay. Here's the situation. You have the employee's names and salaries in
a text file, correct? And you want to update this file. Well, the thing
with text files is, you dont usually just get to update them in place,
because they are variable width. There are normally two ways to deal with
this.
1. Read the entire file into a buffer. Make changes into the buffer. Write
the new file to a new name. Copy over your new file to the old one.
or
2. Read the file line by line. If the line is not effected, write it to a
new file. If it is effected, write the changes to the new file. Continue
til end of file. Copy over your new file to the old one.
Each method has it's advantages. The advantage of 1. is it's easier to
determine where to do an insert as you have the entire file in memory. The
disadvantage is for large files it can take quite a huge buffer. The
advantage to 2 is you only have to buffer one line at a time.
Now. As for you reading the lines into a char[][], this can have problems.
What happens if a line is over 100 characters? What if you have more than
100 employees? I would rather use a vector of std::string. Both are
dynamic.