how to accept input from a file.

Aug 20, 2018 at 3:50pm
I want to make a class whose object has three data members which are firstname,lastname and age. I have inputs in a file with details of one object space separated and next object in next line. How do I read the space separated inputs and store them in firstname,lastname,age respectively?
Aug 21, 2018 at 7:11am
You read the input into your object variables like into normal variables using >>
Instead of cin you use a ifstream;

1
2
3
4
5
6
7
8
9
10
11
12
struct Person
{
  string firstName;
  string lastName;
  int age;
};

int main()
{
  Person p;
  cin >> p.firstName >> p.lastName >> p.age;
}


http://www.cplusplus.com/doc/tutorial/files/
Aug 21, 2018 at 2:04pm
Ok, but what should I do if I want to skip to ith line and take input from there from the input file.
Last edited on Aug 21, 2018 at 2:08pm
Aug 21, 2018 at 3:33pm
Just call getline i-1 times and start the input from there
Topic archived. No new replies allowed.