نوشته شده توسط
amin1softco
دوست من شما کد رو کامل بزارتا بتونیم کمک کنیم
چون توابع find , replace متعلق به یک کلاس به نام file_str هستند و اینجوری نمی شه تحلیلش کرد
ولی معمولا اگر طول رکورد ها ثابت باشه می تونیم موقعیت cursorنوشتن رو به اول رکورد مورد نظر انتقال بدیم و دوباره بازنویسیش کنیم این pos فکر کنم به همین خاطر باشه
اینم کل کد. حرف شما هم درسته و کار pos رفتن به مکان های مختلف رکورد (اول اسم، اول شماره دانشجویی و ...) هست اما چطور؟
// Begin of File
//************************************************
// Programmer : Mohammad Rastkar
// Last Build : APR_11_08
// Compiler : VC++2005 - Win32 Console App
// Purpose : Separate utility to manipulate a 'Class_List' file (add, delete, modify, list Records)
// for using later, with 'Lettered_Numbers' program
//************************************************
/////////////< Includes >/////////////
#include <conio.h> // getch()
#include <fstream>
#include <string>
#include <sys\stat.h> // stat
#include <cerrno> // errno
#include <iostream>
using namespace std;
//////////////< Variables >//////////////
char choice; // for choosing from menu
fstream *fs; // file stream for 'Class_List' file
string file_str; // string as 'Class_List' file
/////////////< Main >/////////////
int main()
{
fs = new fstream( "Class_List.txt", ios::in );
if ( fs->bad() )
{
cout << "\n Can't open or create 'Class_List' file" << endl;
system ( "pause" );
return 1;
}
(*fs) >> file_str; // store whole of the file in 'file_str'
fs->close();
delete fs;
fs=0;
while (1)
{
do // menu
{
system( "cls" ); // clear screen
cout << "\n < Class List > \n\n";
cout << "(1) Add a new student \n";
cout << "(2) Modify an existing student's information\n";
cout << "(3) Delete an existing student \n";
cout << "(4) Display students' informations \n";
cout << "(5) Exit \n\n";
cout << " Enter a choice (1-5) : " << flush;
choice = getch();
} while ( choice < '1' || choice > '5');
system( "cls" );
switch ( choice )
{
int del_num; // number of characters for deletion
int pos; // a position in the string
int rows; // on Displaying, for each 25 rows in the screen there is a pause
case '1' : // Add Student
{
string input; // ID of student
cout << "\n\t\t < Entering a new student > ";
cout << "\n Enter the following informations for the new student : ";
cout << "\n\n ID : ";
cin >> input;
file_str += input + '>'; // '>' -> delimiting students' informations
cout << "\n Name (Only one word) : "; // can be more than one word
cin >> input;
file_str += input + '>';
cout << "\n Student was added." << endl;
system ( "pause" );
break;
}
case '2' : // Modify Student
{
string input;
cout << "\n Enter Student ID, that you want modify its informatin : ";
cin >> input;
pos = file_str.find(input);
if (pos == string::npos)
{
cout << "\n Your specified student doesn't exist in the list" << endl;
system ( "pause" );
break;
}
cout << "\nEnter new informations for this student : ";
cout << "\n\n ID : ";
cin >> input;
del_num = file_str.find('>', pos+1)-pos; // ID : first character of ID to first '>'
file_str.replace (pos, del_num, input); // replace the current ID with the new one
cout << "\n Name (Only one word) : ";
cin >> input;
pos = file_str.find('>', pos+1) + 1; // first character of Name
del_num = file_str.find('>', pos+1)-pos; // Name : first character after first '>' to second '>'
file_str.replace (pos, del_num, input); // replace the current Name with the new one
cout << "\n Record was modified." << endl;
system ( "pause" );
break;
}
case '3' : // Delete Record
{
string input;
cout << "\n Enter student ID, for deletion : ";
cin >> input;
pos = file_str.find(input);
if (pos == string::npos)
{
cout << "\n Your specified student doesn't exist in the list" << endl;
system ( "pause" );
break;
}
file_str.erase ( file_str.begin()+pos, file_str.begin()+file_str.find('>', file_str.find('>', pos+1)+1)+1 );
cout << "\n Student was deleted." << endl;
system ( "pause" );
break;
}
case '4' : // Display Students' Informations
{
cout << "ID\t\tName\n"
<< "-----------------------------------\n";
rows = 3; // 2 rows for header, and 1 for pause message at end
pos = 0;
string substr; // substring for Displaying students' informations
while (pos != file_str.length())
{
substr = file_str.substr( pos, file_str.find('>', pos+1)-pos );
pos = file_str.find('>', pos+1) + 1;
cout << substr;
substr = file_str.substr( pos, file_str.find('>', pos+1)-pos );
pos = file_str.find('>', pos+1) + 1;
cout << "\t\t" << substr << endl;
if (++rows == 25)
{
rows = 3;
system ( "pause" );
system ( "cls" );
cout << "ID\t\tName\n"
<< "-----------------------------------\n";
}
}
cout << "End of the list." << endl;
system ( "pause" );
break;
}
case '5' : // Exit
{
cout << "\n Updating 'Class_List' file..." << flush;
fs = new fstream( "temp.txt", ios::out );
if (fs->fail())
{
cout << "\n Can't create 'temp' file, then 'Class_List' file won't be updated." << endl;
system ( "pause" );
system( "cls" );
return 1;
}
(*fs) << file_str;
fs->close();
delete fs;
fs=0;
struct stat st; // to check that the file exists
int res = stat( "Class_List.txt", &st );
if ( res == 0 ) // if file exists
if( remove( "Class_List.txt" ) == -1 )
{
cout << "\nCan't delete 'Class_List' file, then Updating is incomplete." << endl;
system ( "pause" );
system( "cls" );
return 1;
}
if ( rename( "temp.txt", "Class_List.txt" ) != 0 )
{
cout << "\nCan't rename 'temp' file, then Updating is incomplete." << endl;
system( "pause" );
system( "cls" );
return 1;
}
cout << "\n Updating completed. \n" << endl;
system ( "pause" );
system ( "cls" );
return 0;
break;
}
}
}
}
// End of File
ممنون.