简体   繁体   English

C ++中的随机访问文件

[英]Random Access files in c++

Hi i could not specific Record from my binary file. 嗨,我无法从我的二进制文件中具体记录。 This is the method to list out all records. 这是列出所有记录的方法。

int student :: showall(fstream &fp)
{
    student rec;
    fp.seekg(0,ios::beg);
    int i=0;
    cout<<"Position\tRoll No\t\tName\tBalance"<<endl;
    while(fp.read((char*)&rec,sizeof rec))
    {   
        cout<<i*sizeof rec<<"\t";
        rec.show();
        i++;
    }
}

While running this i got the below output 运行此程序时,我得到以下输出

  Position Roll No Name Balance 0 1 Heartly 10 28 2 Heartly 20 56 3 Heartly 30 84 4 Heartly 40 112 5 Heartly 50 140 6 Heartly 60 168 7 Heartly 70 196 8 Heartly 80 224 9 Heartly 90 252 10 Heartly 100 

Now i want to search specific record. 现在,我想搜索特定记录。 I gave RecNo 5 to search. 我给了RecNo 5搜索。 This is my View method 这是我的View方法

int student :: view(fstream &fp,int RecNum)
{
    student rec;
    std::ios::pos_type SearchPosition = RecNum*sizeof rec;
    cout<<endl<<"Position="<<SearchPosition<<endl;
    if( fp.seekg(SearchPosition) == 0 )
    {
        cout<<endl<<"Position="<<SearchPosition<<endl;
            if(fp.read((char*)&rec,sizeof rec))
                    rec.show();
        else
            cout<<endl<<"Read Error .. ! "<<endl;
        }
    else
    {
        cout<<endl<<"Record Number Not Found"<<endl;
    }   
}

After running this method , i got below result. 运行此方法后,我得到了以下结果。

Record number (-1 Cancels): 5 记录数(-1取消):5

Position=140 位置= 140

Record Number Not Found 找不到记录号

Why it is not locating the specific Record? 为什么没有找到特定的记录?

printf("\nMenu : AddDummy, Add, View, Edit, List or Quit ( U, A, V, E, L or Q) : ");  switch(toupper(getchar()))  {     
     case 'U' :
        file.open("abc.cli",ios::in|ios::out|ios::binary|ios::app);
            cout<<"Creating dummy file of 1000 entries"<<endl;
            record.adddummy(file);
        file.clear();
            break;
    case 'A' :
        file.open("abc.cli",ios::in|ios::out|ios::binary|ios::app);
            record.get();
        record.add(record,file);
            cout<<"Record Added"<<endl;         
            file.clear();
            break;              
    case 'E' :      
            file.open("abc.cli",ios::in|ios::out|ios::binary|ios::ate);
            cout<<endl<<"Record number (-1 Cancels): "<<endl;
            cin>>Rec;
            if (Rec > -1){
                record.get();
         if(!record.update(record,file,Rec))
            cout<<"Record Edited"<<endl;            
             else
            cout<<"Record Edited Failed"<<endl;     
        file.clear();}
             break;
    case 'V' :      
             file.open("abc.cli",ios::in|ios::out|ios::binary|ios::ate);
             cout<<endl<<"Record number (-1 Cancels): "<<endl;
             cin>>Rec;
             if (Rec > -1)
                record.view(file,Rec);
         file.clear();
             break;
    case 'L' :
         file.open("abc.cli",ios::in|ios::out|ios::binary|ios::ate);
             record.showall(file); 
         file.clear();
             break; 
    case 'Q' :
            file.close();
        return 0;   
    default :
        cout<<" \nNot Matching\n ";     
}while(getchar() != '\n');

Why its not reading the specific Record?enter code here 为什么不读取特定的记录?在此处输入代码

fstream::seekg returns a fstream reference, will never be equal 0. So the condition you are checking will never runs. fstream::seekg返回一个fstream引用,永远不会等于0。因此,您要检查的条件将永远不会运行。 fstream::seekg fstream :: seekg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM