FileIO using class with accompanying ccp file

Hi,

I was assigned by my prof to write a program that takes in employee information and use them according to his specifications. He told us to instead of inputting the file from the main cpp file, he told us to do it in a separate header file using a class with two methods to input and output. I've made the class for the Employees and all that but I am unsure of how to start/do my file io files.

I can provide further info when requested.

Thanks
Many times it helps to just write everything in your main .cpp file, get it working, then split the file into stuff.

For example:

main.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

struct Student
{
  std::string surname;
  std::string name;
  int         age;
};

std::istream& operator >> ( std::istream& ins, Student& student )
{
  getline( ins, student.surname );
  getline( ins, student.name );
  ins >> student.age;
  ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}

std::ostream& operator << ( std::ostream& outs, const Student& student )
{
  return outs
    << student.surname << "\n"
    << student.name << "\n"
    << student.age << "\n";
}

int main()
{
  std::vector <Student> students;
  {
    std::ifstream f( "students.txt" );
    Student student;
    while (f >> student) students.emplace_back( student );
  }
  
  std::cout << "The students are:\n";
  for (auto student : students)
    std::cout << "  " << student.name << " " << student.surname << "\n";
}

Once you have it compiling, you can split things into a student.hpp and student.cpp pair of files.

student.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef STUDENTS_HPP
#define STUDENTS_HPP

#include <iostream>
#include <string>

struct Student
{
  std::string surname;
  std::string name;
  int         age;
};

std::istream& operator >> ( std::istream&, Student& );
std::ostream& operator << ( std::ostream&, const Student& );

#endif 

student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <limits>
#include "student.hpp"

std::istream& operator >> ( std::istream& ins, Student& student )
{
  getline( ins, student.surname );
  getline( ins, student.name );
  ins >> student.age;
  ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}

std::ostream& operator << ( std::ostream& outs, const Student& student )
{
  return outs
    << student.surname << "\n"
    << student.name << "\n"
    << student.age << "\n";
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iostream>
#include <vector>

#include "student.hpp"

int main()
{
  std::vector <Student> students;
  {
    std::ifstream f( "students.txt" );
    Student student;
    while (f >> student) students.emplace_back( student );
  }
  
  std::cout << "The students are:\n";
  for (auto student : students)
    std::cout << "  " << student.name << " " << student.surname << "\n";
}

Now you have to compile and link both .cpp files, though. IDK if you are using an IDE or not.

From the command line, frob your compiler as usual, just add the new .cpp file:

MSVC

cl /EHsc /W4 /Ox /std:c++17 main.cpp student.cpp
Clang (or GCC)

clang++ -Wall -pedantic-errors /O3 /std=c++17 main.cpp student.cpp 


If using an IDE, add all your .cpp files to the project and compile as usual.

Hope this helps.
Holy crap dude tysm. This helps a lot.
Topic archived. No new replies allowed.