Modifying data in file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitbyte89
    New Member
    • Nov 2008
    • 26

    #1

    Modifying data in file

    void modifyData()
    {
    int Id;
    char nm[25];
    float pay;


    fstream modData("Depart ment1.txt",ios: :out || ios::in || ios::binary);
    if(!modData)
    {
    cerr<<"file could not be opened"<<"\n";
    }
    else
    {
    cout<<" Enter the ID of department you want to modify :"<<"\n";
    cin>>Id;
    cout<<"Enter Name of Department:";
    cin>>nm;
    cout<<"\n";
    cout<<"Enter Salary of the Department's Employee :";
    cin>>pay;
    cout<<"\n";

    Department d;

    modData.seekg(( Id-1)*sizeof(Depar tment));

    modData.read((c har*)(&d),sizeo f(Department));

    d.setPayrate(pa y);
    d.setName(nm);
    d.setId(Id);

    modData.seekp(( Id-1)*sizeof(Depar tment));

    modData.write(( char*)(&d),size of(Department)) ;
    }


    }

    this is the code to modify data in file but its not working well can any1 help me? please
  • amonsch
    New Member
    • Nov 2008
    • 7

    #2
    Code:
    fstream modData("Department1.txt",ios::out || ios::in || ios::binary);
    you need to use the bitwise inclusive or-operator ( | ) not the relational or-operator( || )

    ---
    amonsch

    Comment

    • bitbyte89
      New Member
      • Nov 2008
      • 26

      #3
      ooohhhhhhhh thank u soooo sooo much ive been trying from last 2 hours n was unable to debug...thanks a lot

      Comment

      • bitbyte89
        New Member
        • Nov 2008
        • 26

        #4
        can any1 please guide how can we delete a record from a file

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          That depends upon what you mean by delete.

          If delete means the record is gone and the space it occupied is also gone, then you will need to rewrite the file.

          If delete means the record is invisible to the user of your read/write functions, then you can fill the deleted record's are with "delete characters". That is, an arrangement of data that means "no data here". The downside here is that you will need to run a reformat utility (which you write) that will copy the file to a temporary location omitting the "delete characters" and then copying the temporary file back to the original file.

          A variation on this is keeping the file in memory, applying the "delete characters" as necessary and then when the file is saved to disc, the save function does not copy the "delete characters" to disc.

          Comment

          • bitbyte89
            New Member
            • Nov 2008
            • 26

            #6
            void delData()
            {
            int id;

            fstream delData("Depart ment1.txt",ios: :out | ios::in|ios::bi nary);
            if(!delData)
            {
            cerr<<"file could not be opened"<<"\n";
            }
            else
            {
            cout<<" Enter the ID of department you want to modify :"<<"\n";
            cin>>id;

            Department d;

            delData.seekg(( id-1)*sizeof(Depar tment));

            delData.read((c har*)(&d),sizeo f(Department));


            delData.seekg(0 ,ios::end);

            int size=delData.te llg()/sizeof(Departme nt);


            int iid=d.getID();
            if(iid!=0)
            {
            Department d1;
            delData.seekp(( id-1)*sizeof(Depar tment));

            delData.write(( char*)(&d1),siz eof(Department) );

            size--;

            }
            else
            cout<<" Id "<<id<<" is already empty "<<"\n";


            Department td[10];

            for(int i=0 ; i<=size ; i++)
            {
            int iD=1;
            delData.seekg(0 );
            td[i].setId(iD);
            int gid=td[i].getID();
            if(gid>0)
            {
            delData.read((c har*)(&td[i]),sizeof(Depart ment));
            iD++;
            }
            else
            continue;
            }
            }
            }



            still its not working....
            it is still printing like
            1
            2
            4
            5

            Comment

            Working...