Question

In: Computer Science

so i want to read in a file. just to keep it simple, the text file...

so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names.

So how do I do this in c++?

Solutions

Expert Solution

Please find the CPP program to read and display the student details.

Program:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

/*class to stote student marks and grades*/
class student
{
public:
string name;
int grade;

//only a constructor to store the name and grade
student(string nam, int grad)
{
name=nam;
grade=grad;
}
};

int main(int argc, char **argv)
{
vector<student> all;
string name;
int grade;
ifstream myfile (argv[1]);

//validation for cmd line arguments
if(argc != 2)
{
cout <<"Usage: "<<argv[0]<<"<input filename>"<<endl;
return -1;
}

/* read the file line by line */
if (myfile.is_open())
{
while ( myfile>>name>>grade )
{
//pass it to new object std1
student std1(name, grade);
//using vectors to populate the student name and grade
all.push_back(std1);
}
//close the text file
myfile.close();
}
else
{
cout << "Unable to open file";
return -1;
}

cout <<"Following are the student details from file: "<<argv[1]<<endl;
for (int i=0; i<all.size(); i++)
{
cout << all[i].name<<" "<<all[i].grade<<endl;
}
return 0;
}

Sample Input file:

Irving 2
James 2
fdsdf 23
sdfsd 123
reer 123
sdfsdf 23
sdfsd 213

Output:

USER>./a.out studentMark.txt
Following are the student details from file: studentMark.txt
Irving 2
James 2
fdsdf 23
sdfsd 123
reer 123
sdfsdf 23
sdfsd 213

Screen Shot:


Related Solutions

For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
please I want it to step by step and in word posted so I can read...
please I want it to step by step and in word posted so I can read them. Q: If the average realized return of a portfolio is 27.5% per year, the standard deviation of returns is 50%, the portfolio beta is 1.25, the average return of Treasury bills over the same period is 2.5% per year, and the average return on the market is 12.5% per year Calculate i) the Sharpe; ii) Treynor and iii) Jensen    
C language problem. Suppose I open I file and read a integer 24. And I want...
C language problem. Suppose I open I file and read a integer 24. And I want to store them into a array byte []. The answer should be byte[0] = 0x9F. How can I do that?
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT