简体   繁体   中英

Read lines from text file and store into array

How do I read lines from a text file and store them into an array. For example I have a text file with 45 different lines.

My attempt:

int main()
{
    int a[45];
    ifstream myfile("enroll_assg.txt");

    if(!myfile){
        cout << "Error opening file" << endl;
        return -1;
    }

    if(myfile.is_open()){
        while(getline(myfile, a, '\n') == 1){

        }
    }

}

I am supposed to make a hash table with this. I get a error in the while loop at the getline(). It is saying "no instance of overloaded functions".

I have 45 lines and each line has white space. Each line looks like:

9650376 George Jones CS 4.5... ...

Student.cpp:

template <typename Comparable>
Student<Comparable>::Student()
{
    // Add your code
    this->fname = fname;
    this->lname = lname;
    this->gpa = gpa;
    this->department = department;
    this->id = id;
    this->bucketId = bucketId;
}

The student.cpp also has the getters and setter for the variables.

A better alternative would be to use std::vector as shown below. The advantage of using a vector over built in array is that since a vector is a dynamically sized container, we don't need to know beforehand how many students/entries will be there in the input file. We can add that information dynamically.

The below shown program uses struct to represent a given Student and it also used a std::vector . You can use this program as a reference(starting point). It reads student information from the input text file and store that information in a vector of Student .


#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
//this class represents a Student
struct Student
{
    std::string firstName, lastName, courseName ;
    unsigned long id = 0;
    float marks = 0;
    
};

int main()
{
   std::ifstream inputFile("input.txt");
   std::string line;
   std::vector<Student> myVec;//create a vector of Student objects
   if(inputFile)
   {
       while(std::getline(inputFile, line))//read line by line
       {
           Student studentObject;
           std::istringstream ss(line);
           //read the id 
           ss >> studentObject.id;
           
           //read the firstname 
           ss >> studentObject.firstName;
           
           //read the lastname 
           ss >> studentObject.lastName;
           
           //read the courseName
           ss >> studentObject.courseName;
           
           //read the marks 
           ss >> studentObject.marks; 
           
           if(ss)//check if input succeded
           {
               myVec.emplace_back(studentObject);//add the studentObject into the vector
           }
       }
   }
   else 
   {
       std::cout<<"File cannot be opened"<<std::endl;
   }
   
   //lets print out the elements of the vecotr to confirm that all the students were correctly read 
   for(const Student &elem: myVec)
   {
       std::cout << elem.id << ": "<<elem.firstName<<" "<<elem.lastName<<" "<<elem.courseName<<" "<<elem.marks <<std::endl;
   }
    return 0;
}

The output of the above program can be seen here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM