Lucy Worrall
Lucy Worrall

Reputation: 1

Create a vector of strings from file c++

I am trying to input three pieces of information from a .txt file.

First column is the course mark. Second column is the course code. Third column(s) is the course name.

I would like to store these as 3 vectors of strings.

Would using stringstream be a good option here? and maybe iterators?

The .txt file is like

65.6 10071   Mathematics 1
66.7 10101   Dynamics
60.0 10121   Quantum Physics and Relativity
66.9 10191   Introduction to Astrophysics and Cosmology
...  ...    ...

and my code so far is

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<string>
#include<iterator>
#include<sstream>
#include<vector>

//Main Function
int main()
{

  //Define variables
  std::string course_mark, course_code, course_name;
  std::vector<std::string> course_mark_vector;
  std::vector<std::string> course_code_vector;
  std::vector<std::string> course_name_vector;

  std::string data_file[100];

  // Ask user to enter filename
  std::cout<<"Enter data filename: ";
  std::cin>>data_file;

  int i{0};

  // Open file and check if successful
  std::fstream course_stream(data_file);

  if(course_stream.is_open()) {
        while (!course_stream.eof()) //while the end of file is NOT reached
        {
            //I have 2
            getline(course_stream, course_mark, ' ');
            course_mark_vector.push_back(course_mark);

            getline(course_stream, course_code, ' ');
            course_code_vector.push_back(course_code);

            getline(course_stream, course_name, '\n');
            course_name_vector.push_back(course_name);
            
            i += 1; //increment number of lines
        }
        course_stream.close(); //closing the file
        std::cout << "Number of entries: " << i-1 << std::endl;
    }

  else{
        std::cout << "Unable to open file. Please run again" << std::endl;
        return 1;
  }

Any help would be greatly appreciated

Upvotes: 0

Views: 834

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595837

Would using stringstream be a good option here?

Yes.

and maybe iterators?

There is no need for iterators in this case.

Try this:

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

//Main Function
int main()
{
    //Define variables
    std::string course_mark, course_code, course_name, data_file, line;
    std::vector<std::string> course_mark_vector, course_code_vector, course_name_vector;
    int i = 0;

    // Ask user to enter filename
    std::cout << "Enter data filename: ";
    std::cin >> data_file;

    // Open file and check if successful
    std::ifstream course_stream(data_file);

    if (!course_stream.is_open())
    {
        std::cout << "Unable to open file. Please run again" << std::endl;
        return 1;
    }

    while (std::getline(course_stream, line)) //while the end of file is NOT reached
    {
        std::istringstream iss(line);

        iss >> course_mark;
        course_mark_vector.push_back(course_mark);

        iss >> course_code;
        course_code_vector.push_back(course_code);

        std::getline(iss >> std::ws, course_name);
        course_name_vector.push_back(course_name);
            
        ++i; //increment number of lines
    }

    course_stream.close(); //closing the file
    std::cout << "Number of entries: " << i << std::endl;

    return 0;
}

Demo

Upvotes: 1

Related Questions