File Handling

if plz anyone can help me through this . i have to read a particular student data from file using his Roll no and modify it and then save it to file again
Plz help me through
This is the File
24 salman 98 97 96 95 94 A ;
21 faizan 88 87 86 83 85 B ;
35 Gohar 99 98 97 96 90 B ;
45 sibghat 91 92 78 85 88 C ;
009 john 89 87 91 78 95 C ;

how can i read data of roll no 35 and modify it and save it again to the file

this is my program


int r = 0 ;
while ( ar[j]!="0" )
{
//cout << "Salman majid is \n" ;
if ( ar[j]==k )
{
r = j ;
getdata() ;

if ( ar[r]!=";" )
{
ostringstream rl;
rl << s[i].roll_no ;
ar[r] = rl.str() ;

r++;
ar[r] = s[i].name ;
r++;
ostringstream p;
p << s[i].p_marks ;
ar[r] = p.str() ;
r++;
ostringstream c;
c << s[i].c_marks ;
ar[r] = c.str() ;
r++;
ostringstream m;
m << s[i].m_marks ;
ar[r] = m.str() ;
r++;
ostringstream e;
e << s[i].e_marks ;
ar[r] = e.str() ;
r++;
ostringstream cs;
cs << s[i].cs_marks ;
ar[r] = cs.str() ;
r++;
ostringstream per;
per << s[i].per ;
ar[r] = per.str() ;
r++;
ar[r] = s[i].grade ;

}
break ;
}
j++ ;
}
//############################################################################################

int b = 0 ;
cout << "after updating :\n" ;
while(ar[b]!="0")
{

cout << ar[b] << " " ;
b++ ;
}

//############################################################################################
ofstream of ("C:/Users/MR_Salman/Desktop/student records.txt",ios::out|ios::binary);
int x = 0 ;
while ( ar[x]!="0" )
{

of<<ar[x] << " ";
if ( ar[x] == ";" )
{
of << "\n\n" ;
}
x++ ;
}
cout << "data is written \n" ;
//cout << ar[x] << " " ;
//x++ ;
}
Please use code tags, it makes the program so much easier to read and to refer to particular lines of code http://www.cplusplus.com/articles/jEywvCM9/

There sees to be a lot missing, the code starts in the middle of something. I don't see any headers, or main(), but importantly, I've no idea what is ar here: while ( ar[j]!="0" ) and I can't see the code for getdata().

But still, this is one way of reading the file:
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>

    using namespace std;
    
const int numscore = 5;

struct Student {
    int id;
    string name;
    int score[numscore];
    char grade;
};

int main()
{
    ifstream fin("student records.txt");
    string line;

    while (getline(fin, line) )
    {
        Student stu;
        // extract data from line into stu
        istringstream ss(line);
        ss >> stu.id >> stu.name;
        for (int i=0; i<numscore; i++)
            ss >> stu.score[i];
        ss >> stu.grade;

        // Display comtents of stu
        cout << setw(4) <<  stu.id << ' '
             << left << setw(15) << stu.name << right;
        for (int i=0; i<numscore; i++)
            cout << setw(4) << stu.score[i];
        cout << "  " << stu.grade << endl;

    }

    return 0;
}

Output:
  24 salman           98  97  96  95  94  A
  21 faizan           88  87  86  83  85  B
  35 Gohar            99  98  97  96  90  B
  45 sibghat          91  92  78  85  88  C
   9 john             89  87  91  78  95  C


You might save all the Student objects in an array or vector, make whatever changes you want, and then write out the details from the array to a file.
Thank you very much bro
This worked great Thank you very Much
Last edited on
Topic archived. No new replies allowed.