Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

PS. I noticed you were using string and ifstream without the std::. This suggests you have using namespace std; in your code. Please don't do that. see Why is "using namespace std;" considered bad practice? Why is "using namespace std;" considered bad practice?

PS. I noticed you were using string and ifstream without the std::. This suggests you have using namespace std; in your code. Please don't do that. see Why is "using namespace std;" considered bad practice?

PS. I noticed you were using string and ifstream without the std::. This suggests you have using namespace std; in your code. Please don't do that. see Why is "using namespace std;" considered bad practice?

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Don't explictly close() a file unless you are going to check that it worked (or are going the re-open). Prefer to let the destructor do the closing (that way it is exception safe). See: My C++ code involving an fstream failed review My C++ code involving an fstream failed review

Don't explictly close() a file unless you are going to check that it worked (or are going the re-open). Prefer to let the destructor do the closing (that way it is exception safe). See: My C++ code involving an fstream failed review

Don't explictly close() a file unless you are going to check that it worked (or are going the re-open). Prefer to let the destructor do the closing (that way it is exception safe). See: My C++ code involving an fstream failed review

added 504 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
struct Person
{
 std::string name;
 std::string age;
 std::string salary;
 std::string hoursWorked;
 std::string randomText;
 friend std::istream& operator>>(std::istream& str, Person& data)
 {
 std::string line;
 Person tmp;
 if (std::getline(str,line))
 {
 std::stringstream iss(line);
 if ( std::getline(iss, datatmp.name, ':') && 
 std::getline(iss, datatmp.age, '-') &&
 std::getline(iss, datatmp.salary, ',') &&
 std::getline(iss, datatmp.hoursWorked, '[') &&
 std::getline(iss, datatmp.randomText, ']'))
 { /* OK: All read operations worked */
 data.swap(tmp); // C++03 as this answer was written a long time ago.
 }
 else
 {
 // One operation failed.
 // So set the state on the main stream
 // to indicate failure.
 str.setstate(std::ios::failbit);
 }
 }
 return str;
 }
 void swap(Person& other) throws() // C++03 as this answer was written a long time ago.
 {
 swap(name, other.name);
 swap(age, other.age);
 swap(salary, other.salary);
 swap(hoursWorked, other.hoursWorked);
 swap(randomText, other.randomText)
 }
};
struct Person
{
 std::string name;
 std::string age;
 std::string salary;
 std::string hoursWorked;
 std::string randomText;
 friend std::istream& operator>>(std::istream& str, Person& data)
 {
 std::string line;
 if (std::getline(str,line))
 {
 std::stringstream iss(line);
 if ( std::getline(iss, data.name, ':') && 
 std::getline(iss, data.age, '-') &&
 std::getline(iss, data.salary, ',') &&
 std::getline(iss, data.hoursWorked, '[') &&
 std::getline(iss, data.randomText, ']'))
 { /* OK: All read operations worked */}
 else
 {
 // One operation failed.
 // So set the state on the main stream
 // to indicate failure.
 str.setstate(std::ios::failbit);
 }
 }
 return str;
 }
};
struct Person
{
 std::string name;
 std::string age;
 std::string salary;
 std::string hoursWorked;
 std::string randomText;
 friend std::istream& operator>>(std::istream& str, Person& data)
 {
 std::string line;
 Person tmp;
 if (std::getline(str,line))
 {
 std::stringstream iss(line);
 if ( std::getline(iss, tmp.name, ':') && 
 std::getline(iss, tmp.age, '-') &&
 std::getline(iss, tmp.salary, ',') &&
 std::getline(iss, tmp.hoursWorked, '[') &&
 std::getline(iss, tmp.randomText, ']'))
 { /* OK: All read operations worked */
 data.swap(tmp); // C++03 as this answer was written a long time ago.
 }
 else
 {
 // One operation failed.
 // So set the state on the main stream
 // to indicate failure.
 str.setstate(std::ios::failbit);
 }
 }
 return str;
 }
 void swap(Person& other) throws() // C++03 as this answer was written a long time ago.
 {
 swap(name, other.name);
 swap(age, other.age);
 swap(salary, other.salary);
 swap(hoursWorked, other.hoursWorked);
 swap(randomText, other.randomText)
 }
};
added 25 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
added 30 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
added 289 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /