Click here to Skip to main content
15,395,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The question : Create the file student.txt with some sample data and design a program
which implements at least TWO (2) functions to read data from the file and
display some information (of your choice) regarding students.

I have created the student.txt file. I can display all data from this file without using function. But the question asked to use function which I don`t really know how to do so. It would be so great if some of you can help me. Thank you.

What I have tried:

This is the code that I`ve created.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

	ifstream myfile;"student.txt",ios::in);	
	if (myfile.is_open()){
		string line;
		while (getline(myfile, line)){
		   cout << line << endl;
return 0;		 
Updated 8-Dec-21 12:43pm
Rick York 8-Dec-21 16:15pm
I think the point is to have some data about each student the file and read the file and store the data. You need to decide what data to have in the file. Examples are last name, first name, identifier, and birthday. You could use commas to delimit each data item.

Your program shows the contents of a file - not necessarily a student, if "student.txt" contained the contents of "Moby Dick", or perhaps was a copy of the C compiler, it would dump that to cout just as happily.
Since the assignment is to create two functions to read and display student information, I suspect what is expected is that you write one function to read student data into a data structure of some sort, and then another function to display some or all of the student information, so for example something like this
struct Student
   // data members for Student object

Student readStudent(string filename)
   // details on how to read a Student omitted

void displayStudent(Student& student)
   // details on how to print out Student omitted

int main()
   Student s = readStudent("student.txt");
If your tutor is asking you to use a Function then you should have already covered that in class - here is a reminder of how to create and use Functions in C++ ... Functions - C++ Tutorials[^]

I suggest having a function that extracts some information from each line of the file - there is an example of doing that by delimiter here - std::getline -[^] and perhaps another one that formats that information for output.
The answer before is with some struct, but plain functions. Depending on the details of your assigment you may consider using classes.

class Student{
  String name;
  read(string filename); // so only one student per file
for that reason I would suggest some
class  StudentFile {
Student* readFile(string filename, int &count); // creates some array of student and ref output of count
Anis Amira 8-Dec-21 12:26pm
Thank you for the help but my lecturer didnt teach us how to use class in C++, so I dont know how to use it here.
KarstenK 9-Dec-21 2:02am
You shouldnt rely on your tutor, but ask him what to do. Maybe it is your homework.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900