【问题标题】:How to find specific string constant in line and copy the following如何在行中查找特定的字符串常量并复制以下内容
【发布时间】:2012-04-15 22:17:54
【问题描述】:

我正在创建一个有点弱/模糊的数据库(我的经验很少,请原谅我的代码混乱)。为此,每次我的控制台程序启动时,我都会创建一个检查,检查是否已经创建了一个数据库(复制到 userlist.txt),如果没有创建一个新的,如果数据库存在,那么它应该全部复制到一个'vector users'(这是一个结构)我在'userbase'类中,然后将包含所有用户信息。

我的 userstats 结构看起来像这样,

enum securityLevel {user, moderator, admin};
struct userstats
{
    string ID;

    string name;
    string password;
    securityLevel secLev;                         
};

我将在这段代码中包含来自文本文件的所有这些信息,

    int main()
    {
        Userbase userbase;  // Class to contain userinformation during runtime.

        ifstream inFile;
        inFile.open("userlist.txt");


        if(inFile.good())
        {
            // ADD DATE OF MODIFICATION
            cout << "USERLIST FOUND, READING USERS.\n";
            userstats tempBuffer;
            int userCount = -1;
            int overCount = 0;

            while(!inFile.eof())
            {
                string buffer;

                getline(inFile, buffer);
                if (buffer == "ID:")
                {
                    userCount++;

                    if (userCount > overCount)
                    {
                        userbase.users.push_back(tempBuffer);
                        overCount++;

                    }
                    tempBuffer.ID = buffer;
                    cout << "ID";           // Just to see if works

                }

                else if (buffer == "name:")
                {
                    cout << "name";         // Just to see if works
                    tempBuffer.name = buffer;

                }
                else if (buffer == "password:")
                {
                    cout << "password";     // Just to see if works
                    tempBuffer.password = buffer;
                }



            }
            if (userCount == 0)
            {
                userbase.users.push_back(tempBuffer);
            }

            inFile.close();
        }
...

我尝试做的是阅读和分析文本文件的每一行。 userlist.txt 的一个示例可能是,

created: Sun Apr 15 22:19:44 2012

mod_date: Sun Apr 15 22:19:44 2012


ID:1d
name:admin
password:Admin1
security level:2

(我知道我还没有将“安全级别”读入程序)

编辑:也可能有更多用户只是跟随列表中前一个用户的“安全级别:x”行。

现在,如果程序读取“ID:1d”行,它应该将其复制到结构中,最后我会将其全部放入向量 userbase.users[i]。然而,这似乎不起作用。它似乎没有抓住任何 if 语句。我以前已经让这种程序工作过,所以我很困惑我做错了什么。我真的可以在这方面使用一些帮助。非常欢迎对代码提出任何其他形式的批评。

问候, 米克尔

【问题讨论】:

  • 是否至少输出“USERLIST FOUND, READING USERS.\n”?
  • 对不起,我应该提到它。是的,如果“userlist.txt”存在。如果需要,我可以将整个程序复制到该站点。
  • 使用if(buffer.fine("ID:")!=string::npos)比较“ID:”是字符串的一部分

标签: c++ struct ifstream


【解决方案1】:

if (buffer == ...) 永远不会是true,因为这些行总是包含每行所包含的属性值以及属性的类型。例如:

ID:1d

getline() 读到这个buffer 将包含ID:1d 所以:

if (buffer == "ID:")

将是false。请改用string.find()

if (0 == buffer.find("ID:")) // Comparing to zero ensures that the line
{                            // starts with "ID:".
    // Avoid including the attribute type
    // in the value.
    tempBuffer.ID.assign(buffer.begin() + 3, buffer.end());
}

正如jrok 所评论的,用于读取文件的while 不正确,因为在getline() 之后没有立即进行检查。改为:

string buffer;
while(getline(inFile, buffer))
{
    ...

【讨论】:

  • 另外,while(!inFile.eof()) 可能会中断。见这里stackoverflow.com/questions/21647/…
  • 非常感谢您。 '0 ==' 非常有用,我之前不知道如何实现。并感谢有关 .eof() 的提醒。我也会实现的!
猜你喜欢
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2021-09-20
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
相关资源
最近更新 更多