Write a C++ program that read student’s subject marks from file named (std_makrs.txt) which contains the marks of 5 students. Each row of the file represents the subject marks of a student. Total subject registered for each student is saved into file named (std_subject.txt)
/*
std_makrs.txt file:
75 65 85 95 56
45 56 23
89 56 56 23 56 89 78 45
23 56 89 56 23 15
65 95 75 85
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main () {
ifstream std_makrsFile("std_makrs.txt");
if (std_makrsFile.good())
{
ofstream std_subjectFile;
std_subjectFile.open("std_subject.txt");
string line;
int n=1;
while(getline(std_makrsFile, line))
{
istringstream ss(line);
int totalSubjectRegistered=0;
int mark;
//Read from file in till end of line
while(ss >> mark)
{
totalSubjectRegistered++;
}
std_subjectFile<<"Total subject registered for student "<<n<<" is "<<totalSubjectRegistered<<"\n";
n++;
}
std_subjectFile.close();
}else{
cout<<"\nThe file does not exist.\n\n";
}
//Close the files
std_makrsFile.close();
system("pause");
return 0;
}
Comments
Leave a comment