Answer the question
In order to leave comments, you need to log in
Why does C++ fail to read using fstream an object written to a file using the same fstream?
I am trying to create a pseudo messenger. My messenger has an internal User class. When I register a user, I write it as an object of the User class in a separate file "RegisteredUsers.txt". I did this so that - let's say if I wrote this user into a file, let the name be user1, phone number - 1234, I will register another user, for example, user2's name, number 7890 - so that user2 can click "Add contact" and search for someone by number -something (Meth I will add later) and let's say user2 found user1 by number 1234, and so user2 will be able to write a message to user1. And if user2 entered the wrong number to get out an error that "there is no such user". That's why I write users to the file so they can find each other. But here's the trouble - you can't read an object from a file. I can write it down
I used reading to check if there is already an account with this number, if there is, according to my idea, an error should be displayed.
The first user is successfully registered, and when I try to register the second one, this error comes out in that particular check. Pay attention - when I write the first user, this error does not come out, because fstream (in) can only open an existing file, f.is_open() gives false and the check does not work. When I try to register the second one, the check is already triggered, since when we recorded the first fstream (out) created the file.
#include <iostream>
using namespace std;
#include <fstream>
class Messenger
{
public:
void to_register(string newUserName, string newUserPhoneNumber)
{
/*path = "RegisteredUsers.txt";
f.open(path, fstream::in);
if (f.is_open())
{
User tempUser;
while (f.read((char*)&tempUser, sizeof(User)))
{
}
usersCount = tempUser.ID;
}
else
{
}
f.close();*/
path = "RegisteredUsers.txt";
f.open(path, fstream::in);
if (f.is_open())
{
User tempUser;
while (f.read((char*)&tempUser, sizeof(User)))
{
if (tempUser.phoneNumber == newUserPhoneNumber)
{
cout << "Error. User with this phone number already exists.";
return;
}
}
}
else
{
}
f.close();
User newUser(newUserName, newUserPhoneNumber);
path = "RegisteredUsers.txt";
f.open(path, fstream::out | fstream::app);
if (f.is_open())
{
f.write((char*)&newUser, sizeof(User));
}
else
{
}
f.close();
}
private:
class User
{
public:
User()
{
name = "";
phoneNumber = "";
}
User(string name, string phoneNumber)
: name(name), phoneNumber(phoneNumber)
{
}
string name;
string phoneNumber;
};
fstream f;
string path;
};
int main()
{
setlocale(LC_ALL, "ru");
Messenger Telegram;
Telegram.to_register("Aeris", "+994703228276");
return 0;
}
Answer the question
In order to leave comments, you need to log in
string can't be written like that. Because the class itself stores pointers to data. And its sizeof() is always the same, no matter how long the string is. You wrote some addresses to a file, and when you read them, there is garbage there. Therefore, an error occurs on some internal mechanisms of the line.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question