Creating class from text file inputs

Hello . I was wondering how I would go about doing this

I have a function that reads the text file , and have created the class structure before hand

But I can't think of a way to create a new class ( ie student student(ncount) ) and store the said line from text file into it

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
void loadnames()
{
	FILE *txtfile;
	char line[25];
	int ncount = 0;

	if ( ( txtfile = fopen( "names.txt", "r" ) ) == NULL )
		exit (-1);

	cout << "The names in the text file are:" << endl;
	cout << endl;

	while (  fgets( line, 25, txtfile ) != NULL )
	{
		cout << line ;
		ncount++;
	}

	cout << endl;
	cout << "There are " << ncount << " names on the list\n";


	fclose( txtfile );
	cin.get();
}


Just a nudge in the right direction if you would be so kind

Thanks :)
Last edited on
Perhaps you should use a vector ( http://cplusplus.com/reference/stl/vector/ ). Vectors are similar to arrays, except their size can change as needed and they are MUCH more versatile. Here is how it could be done:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class student {
    public:
        student(string nm){
            name = nm;}
        string name;
    };

vector<student> students;
void load_info(){
    ifstream file("file.txt");
    string temp;

    while (file.good()){
        getline(file, temp);
        students.push_back(student(temp));}}

int main(){
    load_info();}
Thanks , will do :)
Topic archived. No new replies allowed.