在读取文件的循环之后丢失指针向量中的值(使用dirent)

时间:2017-11-05 03:20:26

标签: c++ file pointers vector

我正在编写一个C ++程序,其行为类似于Twitter服务。该程序应该按如下方式将数据存储在文件中:

f_username(存储用户名所遵循的用户),因此它为每个跟随某人的用户创建一个文件。我有函数load_users(),它读取每个文件以填充类型为struct(下面定义)的名为client_db(全局变量)的向量中的所有客户端(用户)信息。因此,当读取f_username文件时,将为每个用户填充向量client_followers和client_following。除了读取的第一个文件外,该函数完美运行。它将信息保存在client_followers中,client_following保存在读取文件的while循环中,但是在此循环之后以某种方式删除了两个向量的条目。我在代码中添加了注释,我只是为它读取的第一个f_username文件面临这个问题。

我认为它与我使用指针的事实有关,但我不知道如何解决这个问题。这是我第一次遇到这种奇怪的逻辑错误。怎么可能它只是从第一个文件中读取的向量“删除”数据,而不是从其他文件中删除(我有3个文件)?

任何帮助都会非常感激,我尽力解释这个问题。

**更新:通过将字符串类型的client_followers和client_following替换为字符串类型而不将其声明为指针来确定问题,并确保client_db中的实际条目正确更新。 **

    //Client struct that holds a user's username, followers, and users they follow
struct Client {
    std::string username;
    bool connected = false;
    int following_file_size = 0;
    std::vector<Client*> client_followers;
    std::vector<Client*> client_following;

    ServerReaderWriter<Message, Message>* stream = 0;

    bool operator==(const Client& c1) const{
        return (username == c1.username);
    }
};

void load_users()
{
    //load users (followers and following)
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir ("./")) != NULL) {
        while ((ent = readdir (dir)) != NULL) {
            std::string filename = ent->d_name;
            int index = filename.find("f_");
            if(index != std::string::npos)
            {

                std::ifstream in; //read following file of username

                //get username
                std::string user = filename.substr(index+2);
                int txt_index = user.find(".txt");
                user.erase(txt_index,4);

                int user_index = find_user(user); //check if user is in client's db already
                //client_db is a global variable
                if(user_index < 0){ //if not, add client to client_db
                    Client c;
                    c.username = user;
                    client_db.push_back(c);
                }

                Client* user1 = &client_db[find_user(user)];
                std::string username2;

                in.open("./" + filename);
                while(!in.eof())
                {
                    getline(in,username2);

                    int following_index = find_user(username2);
                    if(following_index < 0) //create entry for the user
                    {
                        Client c2;
                        c2.username = username2;
                        client_db.push_back(c2);
                    }
                    Client* user2 = &client_db[find_user(username2)];
                    //it adds the information for the first file, but entries in client_followers
                    //and client_following is deleted after this loop ends, how is this possible?
                    user2->client_followers.push_back(user1);
                    user1->client_following.push_back(user2);

                }
                //When I print information of client_following and client_followers, the information obtained from the first file is not printed (e.g. size of client_following is 0).
                in.close();
                //break;
            }
        }
        closedir (dir);
    } else {
        /* could not open directory */
        //perror ("");
        //return EXIT_FAILURE;
        return;
    }
}

0 个答案:

没有答案