2012-05-11 1 views
0

레코드를 상대 파일에 추가 한 후 사용자가 계정 번호를 제공하는 레코드 (클라이언트)의 한 필드 (잔액)를 업데이트하려고합니다. 파일에서 업데이트가 발생하지만 제대로 수행되지 않습니다. 결과는 업데이트가 다른 데이터에 영향을 주었고 쓰레기도 함께 나온다는 것을 보여줍니다. 나는 그 문제의 원인을 알 수 없다. 귀하의 도움을 주시면 감사하겠습니다. Dev-C++을 사용하고 있습니다. 출력 된 코드는 다음과 같습니다. 하, 하, 하, 우리는 같은 클래스에 있어야합니다은 상대 파일 C++의 레코드 필드를 업데이트 할 수 없습니다. C++

#include <iostream> // cin, cout 
#include <iomanip> 
#include <fstream> 
#include <conio.h> 
using namespace std; 


#define SIZE 10 

struct  client  // Client record 
{ int account;  // from 1 to SIZE 
    char name[20]; 
    double balance; 
}; 


void show_file(char filename[]) // Sequential display of all records 
{ 
    client c; 
    int n=0; 
    void *ptr; 

    ifstream IS(filename, ios::in); // Open for sequential read 
    if(!IS) {cerr << filename<< " file open error." << endl; exit(1);} 

    cout << "\n\nSHOW_FILE: The contents of file " << filename; 

    while(ptr=IS.read((char *)&c, sizeof(c))) 
    { 
    cout <<'\n'<< setw(3)<< ++n << setw(6) << c.account <<setw(20) 
     << c.name << setw(10) << c.balance; 
    } 

    IS.close(); 
} 


int main(void) 
{ client c; 
    void *ptr; 
    int n=0, acc,number_of_records=SIZE, field1; 
    double new_balance, field3; 
    char *fname = "credit.dat"; char field2; 


    cout << "\nMAKE_FILE: Creating a blank relative file " << fname 
    << " containing " << number_of_records << " records."; 
    fstream iof(fname, ios:: in | ios::out | ios::binary); 
    if(!iof) {cerr << "File open error." << endl; exit(1);} 

    client blank={0, "", 0.0}; // Create an empty client record 
    while(number_of_records--) 
    iof.write((char *)&blank, sizeof(blank)); 
    cout << "\n\n\nFile has been succesfully created!"; //file is still empty, no records yet. 

    cout<<"\n\nenter the 10 customers into the file: "<< fname<<endl<<endl; 

    cout << "\nAccount[1.." << SIZE 
    << "], Name, Balance (0 0 0 to exit)= "; 
    cin >> c.account >> c.name >> c.balance; 

    while(0 < c.account) // && c.account <= maxrec) 
    { 
    iof.seekp((c.account-1) * sizeof(client)); // position the pointer 
    iof.write((char *)&c, sizeof(c)); 
    cout << "Account[1.."<< SIZE 
     << "], Name, Balance (0 0 0 to exit)= "; 
    cin >> c.account >> c.name >> c.balance; 
    } 

    cout << "\n\nAccount number to apply changes on balance(0 to exit) = "; 
    cin >> acc; 
    /// while(0 < acc && acc <= SIZE) 
    if (0<acc && acc <= SIZE) 
    { 
    //cout << "\nPositioning at " << (acc-1) * sizeof(client)<< endl; 
    iof.seekg((acc-1) * sizeof(client)); // position the pointer 
    iof.read((char *)&c, sizeof(c)); 
    if(c.account) 
    cout <<'\n'<< setw(6) << c.account <<setw(20) 
        << c.name << setw(10) << c.balance; 

    new_balance=c.balance+0.05*(c.balance); //calculation of the new balance by adding interests of 5% 

    cout<<"\n\n\nnew balance after the 5% interest:"<<new_balance<<endl; 
    c.balance=new_balance; 
    cout<<"current new balance: "<<c.balance<<endl; //just to check if it will be displayed 

    //WHERE THE PROBLEM IS... 
    iof.seekg(0, ios::cur); //trying to stay in the current position to apply 
          //change on current balance 
    iof<<c.account << c.name << c.balance; //trying to update record with new balance 

    } 
    else cout << "\nEmpty record"; 


    iof.close(); 
    cout<<"\n\nFILE after THE UPDATE: "<<endl; 
    show_file (fname); 



    cout << "\n\n"; 
    system("pause"); 
    return 0; 

} 


*********************output************************** 

MAKE_FILE: Creating a blank relative file credit.dat containing 10 records. 


File has been succesfully created! 

enter the 10 customers into the file: credit.dat 


Account[1..10], Name, Balance (0 0 0 to exit)= 1 aaaa 2399 
Account[1..10], Name, Balance (0 0 0 to exit)= 2 bbbb 4000 
Account[1..10], Name, Balance (0 0 0 to exit)= 3 cccc 50 
Account[1..10], Name, Balance (0 0 0 to exit)= 4 dddd 5000 
Account[1..10], Name, Balance (0 0 0 to exit)= 5 eeee 180 
Account[1..10], Name, Balance (0 0 0 to exit)= 0 0 0 


Account number to apply changes on balance(0 to exit) = 3 

3    cccc  50 


new balance after the 5% interest:52.5 
current new balance: 52.5 


FILE after THE UPDATE: 


SHOW_FILE: The contents of file credit.dat 
    1  1    aaaa  2399 
    2  2    bbbb  4000 
    3  3    cccc  50 
    41667457843    c52.5♫'  5000 
    5  5    eeee  180 
    6  0        0 
    7  0        0 
    8  0        0 
    9  0        0 
10  0        0 

Press any key to continue . . . 
+0

"상대 파일"이 무엇인지 자세히 설명해 주실 수 있습니까? 또한 디버거 나 printf 등을 사용하여 쓰고 읽는 위치를 추적하여 불일치가 있는지 확인 했습니까? – PlasmaHH

+0

필자는 파일에서 읽고 쓰는 파일 위치 포인터를 사용하는 파일이라고 생각합니다. 디버거를 사용하여 포인터 위치를 추적하려고합니다. – T4000

답변

1

...

당신은 쓰기 포인터의 위치를 ​​읽을 수있는 포인터와 seekp을() 위치를 seekg()를 사용합니다. 이전 파일에서 그랬던 것처럼이 같이

:

iof.seekp ((c.account-1) *는 sizeof (클라이언트)); // 포인터를 위치시킨다. iof.write ((char *) & c, sizeof (c)); requ에

행운을 -

seekg = seekp를 얻기 위해 추구 = 넣어 추구!

관련 문제