file updation
Feb 10, 2018 at 12:04pm UTC
What's wrong with this file updation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cout<<"Update your file" <<endl;
string sh;
cout<<"Enter character to search" ;cin>>sh;
cout<<endl;
fstream r2;
r2.open("marks.dat" ,ios::in|ios::out|ios::binary);;
r2.seekg(0);
while (r2>>line)
{
if (line==sh)
{
cin>>name;
cin>>rollno;
}
r2<<name<<rollno;
}
r2.close();
If you want full code here it is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
fstream w1;
w1.open("marks.dat" ,ios::out|ios::binary);
char name[80],ans;
int rollno;
do
{
cout<<"Enter name" ;cin>>name;
cout<<endl;
cout<<"Enter roll number" ;cin>>rollno;
cout<<endl;
w1<<name<<'\t' <<rollno<<'\n' ;
cout<<"Enter more records?(y/n)" ;cin>>ans;
cout<<endl;
}while (ans=='y' );
w1.close();
cout<<"Contents of your file" <<endl;
string line;
fstream r1;
r1.open("marks.dat" ,ios::in|ios::binary);
r1.seekg(0);
while (r1>>line)
{
cout<<line;
cout<<endl;
}
r1.close();
cout<<"Update your file" <<endl;
string sh;
cout<<"Enter character to search" ;cin>>sh;
cout<<endl;
fstream r2;
r2.open("marks.dat" ,ios::in|ios::out|ios::binary);;
r2.seekg(0);
while (r2>>line)
{
if (line==sh)
{
cin>>name;
cin>>rollno;
}
r2<<name<<rollno;
}
r2.close();
cout<<"Contents of your file" <<endl;
string liner;
fstream r3;
r3.open("marks.dat" ,ios::in|ios::binary);
r3.seekg(0);
while (r3>>liner)
{
cout<<liner;
cout<<endl;
}
r3.close();
}
Last edited on Feb 10, 2018 at 12:09pm UTC
Feb 10, 2018 at 12:41pm UTC
> What's wrong with this file updation
¿symptoms?
Feb 10, 2018 at 12:48pm UTC
in place of original roll no entered new name+roll are displayed
Feb 10, 2018 at 10:18pm UTC
1 2 3 4 5 6 7 8 9
while (r2>>line)
{
if (line==sh)
{
cin>>name;
cin>>rollno;
}
r2<<name<<rollno; //this always execute
}
you are overwritting all the records with `name' and `rollno'
when you do `r2>>line' your position is at the end of the word, that's where you start writting.
¿why did you open the file in binary mode? ¿how much space do you hae in each register?
Feb 14, 2018 at 7:19am UTC
oh ok i got it now
Topic archived. No new replies allowed.