12345678910111213141516171819202122232425
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(); }
1234567891011121314151617181920212223
#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();}