Text file read and write

closed account (1vf9z8AR)
How do i make my program more efficient and eliminate the need of 'end of file'?(line 34)

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>
using namespace std;
struct student
{
    char name[80];
    int rno;
};
int main()
{
        student s1;
    int option,n;
    cout<<"Enter 1 to write file\nEnter 2 to read file";cin>>option;

    if(option==1)
    {
    ofstream f1;
    f1.open("file.dat",ios::binary);
    cout<<"Enter number of customers";cin>>n;
    for(int i=0;i<n;i++)
    {
    cout<<"\nEnter name(dot to terminate)";cin.getline(s1.name,80,'.');
    cout<<"\nEnter roll number";cin>>s1.rno;
        f1.write((char*)&s1,sizeof(student));
    }
      f1.close();
    }

    else if(option==2)
    {
           ifstream r1;
           r1.open("file.dat",ios::binary) ;
             r1.read((char*)&s1,sizeof(student));
           while(!r1.eof())
           {
           cout<<"Name"<<s1.name<<endl;
           cout<<"Roll number"<<s1.rno<<endl;
                    r1.read((char*)&s1,sizeof(student));
           }
           r1.close();
    }

               return 0;
}
Last edited on
I don't know that this is more efficient, it is the same logic, with a few adjustments. No use of eof() - though in the original code it was ok. No need to use a dot to end a string. Note the mixing of cin >> and cin.getline often causes problems. Here I used a function empty() to remove the trailing newline '\n' left behind after the previous cin >>.
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
#include <iostream>
#include <fstream> 
#include <limits>

using namespace std;

struct student
{
    char name[80];
    int rno;
};

void empty()
{
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

int main()
{
    student s1;
    int option,n;
    cout << "Enter 1 to write file\nEnter 2 to read file "; cin >> option; empty();

    if (option==1)
    {
        ofstream f1("file.dat", ios::binary);
        cout << "Enter number of students "; cin >> n; empty();
    
        for (int i=0; i<n; i++)
        {
            cout << "\nEnter name "; cin.getline(s1.name, sizeof(s1.name) );
            cout << "\nEnter roll number "; cin >> s1.rno; empty();
            f1.write((char*)&s1, sizeof(student));
        }
    }

    else if (option==2)
    {
        ifstream r1("file.dat", ios::binary) ;
           
        while (  r1.read((char*)&s1, sizeof(student))  )
        {
            cout << "       Name " << s1.name << endl;
            cout << "Roll number " << s1.rno  << endl;
        }
    }

}
closed account (1vf9z8AR)
i use dot to end input to remove any sort of error as i like to mix cin and cin.getline( errors like infinite output , error in reading, reading input as a single character).
I like how you used the condtion of reading the file inside while loop
Last edited on
i use dot to end input to remove any sort of error as i like to mix cin and cin.getline

That approach might add more problems than it solves. At any rate, it is risky.

Suppose at the prompt "Enter name" the user enters something like
Bjarne

Stroustrup


.

Or perhaps
A
 r
  t
   h
    u
     r.

Your version would accept the whole thing, complete with newlines as part of the name. I can think of circumstances where this type of user input might be useful, but I'm not convinced that this is one of them.

Whatever you decide, you should at least be familiar with more conventional alternatives such as the approach I used.
Topic archived. No new replies allowed.