So, I am trying to read name, number and adress of a contact from a text file.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include "stdafx.h"
#include "Person.h"
#include "PhoneBook.h"
void readFromFile(string filename)
{
int n = 0, i;
string temp_name, temp_number, temp_adress, nstr;
ifstream fin(filename.c_str());
getline(fin, nstr);
n = atoi(nstr.c_str());
for (i = 0; i < n; i++)
{
getline(fin, temp_name);
getline(fin, temp_number);
getline(fin, temp_adress);
contactbook->addPerson(temp_name, temp_number, temp_adress);
}
}
main passes the file name. But I am not sure why this error appears:
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
I think that you are using Visual studio that has its own compiler. As the documentation explain the function string::getline has this declaration
template<class _E, class _TYPE, class _A> inline
basic_istream<_E, _TYPE>& getline(
basic_istream<_E, _TYPE>& Istream,
basic_string<_E, _TYPE, _A>& Xstring,
const _E _D=_TYPE::newline( )
);
so you are missing a parameter that in your case is '\\n'. Try this code
void readFromFile(string filename)
{
int n = 0, i;
string temp_name, temp_number, temp_adress, nstr;
ifstream fin(filename.c_str());
getline(fin, nstr,'\n');
n = atoi(nstr.c_str());
for (i = 0; i < n; i++)
{
getline(fin, temp_name,'\n');
getline(fin, temp_number,'\n');
getline(fin, temp_adress,'\n');
}
}
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.