Hi,
I want to edit a record inside a txt file. So first a search for the record inside the file and display the record to the user. But for example, if the user want to edit the name, how I can go to that position?
void editar_cliente()
{
int Cli_num = 0, seleccion = 0, pointer_Pos;
string Cli_nombre, Cli_apellido, Cli_dirr, dummy, file_record, Num_client_edit, file_record_num;
double Cli_total_compras = 0.0, Cli_balance = 0.0;
ifstream readFile;
readFile.open("Tbl_cliente.txt", ios::in); // open a file for input
readFile.unsetf(ios::skipws);
cout << "Please enter the number of the client you want to edit: ";
cin >> Num_client_edit;
if (!readFile.fail())
{ // if began
while(getline(readFile, file_record, '#'))
{ // while began
cout << file_record_num;
if (Num_client_edit == file_record)
{
getline(readFile, Cli_nombre, '#');
getline(readFile, Cli_apellido, '#');
getline(readFile, Cli_dirr, '#');
readFile >> Cli_total_compras;
readFile.close();
}
// else
// cout << "Cliente #: " << Num_client_edit << " no se ecuentra" << endl;
} // while end
} // if end
else
{
cerr << "Error Opening File!\a" << endl;
}
cout << "\n1. Client Name: " << Cli_nombre << endl;
cout << "2. Last Name : " << Cli_apellido << endl;
cout << "3. Address: " << Cli_dirr << endl;
cout << "4. Total Purchases : " << Cli_balance << endl;
cout << "5. Balance : " << Cli_total_compras << endl;
cout << "\nWhich field you want to edit?: ";
cin >> seleccion;
getline(cin,dummy,'\n');
ofstream file;
file.open("Tbl_cliente.txt", fstream::out);
if (seleccion == 1)
{
cout << "Enter the new name: ";
getline(cin,Cli_nombre,'\n');
}
}
The Tbl_cliente.txt looks like:
1#Juan#Martinez#PO Box 356#0#0#
2#Pedro#Perez#PO Box 32480#0#
Any input would be appreciate.