prblem in building code

Hi everyone

I was doing this question in data handling that searches through a file for records maintained through structures. I wrote this code, but it is not building:

My code:

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
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct stu{
    int roll_no;
    char name[25];
    float marks;
    char grade;
}s;

int main()
{
    system("cls");

    int rno;
    char found = 'n';
    ifstream filein;
    filein.open("student.txt", ios::in);     //assuming student.txt already exists on the disk, in the directory of the main.cpp file
    cout << "\nEnter roll number of student whose records you want to search for : ";
    cin >> rno;

    while(!filein.eof()){
        filein.read((char* ) &s, sizeof(stu));
        if(s.roll_no == rno){
            cout << "\nStudent details:-\n";
            cout << "\nName: " << s.name << endl;
            cout << "Roll No.: " << s.roll_no << endl;
            cout << "Marks: " << s.marks << endl;
            cout << "Grade: " << s.grade << endl;
            found = 'y';
            break;
        }
    }

    filein.close();

    getchar();
    return 0;
}


The error:
error: aggregate 'std::ifstream filein' has incomplete type and cannot be defined


Can anyone throw som light on the problem with the code?(I am using Code::Blocks with MinGw as usual)

Thank You
Last edited on
#include <fstream>
thx Peter87!
what a fool i am
Yes Null
I was compiling without including fstream. I guess I should be more careful before I post queries.

Sorry for this silly question.
But guys the code is not giving the correct output; if roll number 3 is on my student.txt, it should display but the program is saying it can't find roll 3.
How did you create the student.txt file. If you read/write binary data you should open the file in binary mode.
filein.open("student.txt", ios::binary); // ios::in is not necessary when using std::ifstream.
@Peter87


I first created the file in notepad. I opened it in ios::in mode.
filein.read((char* ) &s, sizeof(stu));
This will read sizeof(stu) bytes from the file and store them in s as-is. It will not handle text in any special way or care what data types s actually contains. It just copies bytes.

If student.txt is a text file you will have to read it using << (and possibly other functions) the same way you read data from cin.
Topic archived. No new replies allowed.