This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey dudes! I am amazed by this code today:
void saveContacts(vector<Contact> &contacts)
{
  ofstream contactFile{CONTACT_FILE, ios::out};
  // contactFile.seekp(0, ios::beg);
  if (!contactFile)
    cout << "File couldn't be opened for writing\n";
  else
  {
    for (size_t i = 0; i < contacts.size(); i)
    {
      contactFile << contacts[i].firstName << DELIM;
      contactFile << contacts[i].lastName << DELIM;
      contactFile << contacts[i].phoneNumber << DELIM;
      contactFile << contacts[i].emailAddress << "\n";
    }
  }
  contactFile.close();
}
I am wondering about the file not being overwrritten even though it is opened in out mode. Why does this happen. I am totally happy with this behaviour because that's what I wanted, but that has come unintentionally. What are the possible reasons that this is happening??
// contactFile.seekp(0, ios::beg);
whether the above file is commented out or not, the result is same. Please give me the reason, because I wanted this behavior but not on the mercy of hidden bugs.
Subreddit
Post Details
- Posted
- 4 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/AskProgramm...