【问题标题】:Code optimization question for pulling entry from txt file in c++从 C++ 中的 txt 文件中提取条目的代码优化问题
【发布时间】:2019-03-29 10:06:05
【问题描述】:

我在下面开发了一些代码,这些代码将从列表中提取条目并构建一系列条目以供以后使用。我使用的列表和代码如下。

使用 ifstream 函数,我从列表中抓取字符,每次代码到达下一行字符或\n,代码将编译文本,构建字符串并移至下一行。

这对于从 txt 文件中提取是否有效,它有效但感觉效率低。

文本文件列表

name
title
year
manufacturer
developer
genre
cloneOf
players
ctrltype
buttons
joyways
rating
score

我正在尝试优化的当前代码

#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>


std::string sql;
std::string Entryname;
std::string Entryappends;

int main()
{
    std::ifstream MetaEntries ("c:\\CodeRepo/List.txt", std::ifstream::in);
    sql.append("CREATE TABLE IF NOT EXISTS Meta(");
    sql.append("collectionName TEXT KEY,");

    char c = MetaEntries.get();

    while (MetaEntries.good()) {
        if (c == '\n') {
            Entryappends = std::string(" TEXT NOT NULL DEFAULT '', ");
            sql.append(Entryname + Entryappends);
            Entryname.clear();
        }

        Entryname += c;
        c = MetaEntries.get();
//      std::cout << Entryname;
    }

    MetaEntries.close();
    Entryappends = std::string(" TEXT NOT NULL DEFAULT '';");
    sql.append(Entryname + Entryappends);

    sql.append("CREATE UNIQUE INDEX IF NOT EXISTS MetaUniqueId ON Meta(collectionName, name);");
    std::cout << sql;

    return 0;
}

输出:几乎完全按照预期工作。

CREATE TABLE IF NOT EXISTS Meta(collectionName TEXT KEY,name TEXT NOT NULL DEFAULT '',
title TEXT NOT NULL DEFAULT '',
year TEXT NOT NULL DEFAULT '',
manufacturer TEXT NOT NULL DEFAULT '',
developer TEXT NOT NULL DEFAULT '',
genre TEXT NOT NULL DEFAULT '',
cloneOf TEXT NOT NULL DEFAULT '',
players TEXT NOT NULL DEFAULT '',
ctrltype TEXT NOT NULL DEFAULT '',
buttons TEXT NOT NULL DEFAULT '',
joyways TEXT NOT NULL DEFAULT '',
rating TEXT NOT NULL DEFAULT '',
score TEXT NOT NULL DEFAULT '';CREATE UNIQUE INDEX IF NOT EXISTS MetaUniqueId ON Meta(collectionName, name);

【问题讨论】:

标签: c++


【解决方案1】:

我认为您可以尝试read word by word,或者根据您的文本文件的结构,甚至可以尝试line by line。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用getline

    [...]
    std::ifstream metaEntries ("c:\\CodeRepo/List.txt", std::ifstream::in);
    sql.append("CREATE TABLE IF NOT EXISTS Meta(");
    sql.append("collectionName TEXT KEY,");
    
    string line = "";
    while (std::getline(metaEntries, line)) {
        // don't use global variables. This should probably be a constant:
        std::string entryAppends = std::string(" TEXT NOT NULL DEFAULT '', ");
        sql.append(line + entryAppends);
    }
    if (cin.bad()) {
        // IO error
    } else if (!cin.eof()) {
        // format error (not possible with getline but possible with operator>>)
    } else {
        // format error (not possible with getline but possible with operator>>)
        // or end of file (can't make the difference)
    }
    
    MetaEntries.close();
    [...]
    

    convention 也可以使用小写的变量名,大写的类、结构和枚举,以及所有大写的常量。

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多