【问题标题】:How to update contents of an already existing cout statement如何更新已经存在的 cout 语句的内容
【发布时间】:2019-11-12 06:27:25
【问题描述】:

我正在使用 c++ 中的 mySql 创建一个应用程序,问题就像我们可以在网站上编辑我们的帖子和 cmets,我想从 mySql 表中获取一个单元格并想要编辑特定的单元格。

例如:我正在获取一条记录,上面写着“嘿,这是一个测试帖子”,当我在我的应用程序中获取记录时,用户可以将帖子更新为“嘿,这是一个新帖子”。

现在的问题是,我无法更新 cout 语句。 如何获取记录、显示并在显示时修改其内容?

我尝试使用 strcpy 并通过获取记录并将其复制到字符串中来将其保存在本地字符串中,但这并没有按预期工作

void editPost(){
system("cls");
string dummy;
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
conn = mysql_init(0);
conn = mysql_real_connect(conn, "192.168.0.110", "admin", "admin", "search_engine", 0, NULL, 0);
string strBuff[1000];
if(conn){
    int qstate = 0;
    int id;
    cout << "Enter id : ";
    cin>>id;
    stringstream ss;
    ss<<"SELECT content FROM se__dbms where id = '" << id <<"'";
    string query = ss.str();
    const char* q = query.c_str();
    if(conn){
        int qstate = mysql_query(conn, q);

        if(!qstate){
            res = mysql_store_result(conn);
            while(row = mysql_fetch_row(res)){
                    strBuff[1000]=row[0];
            }
            cout<<strBuff[1000]; /*problem over here, 
                                  what can i change in this cout statement 
                                  that it will even display the contents,
                                  and the user can edit it as well */
            ss << "UPDATE se__dbms SET content = " << strBuff << " WHERE id = '" << id << "'";
        }
    }
    if(qstate == 0){
        cout << "Record Updated..." << endl;
        cout << "Press B to go back";
        cin >> dummy;
    }
    else{
        cout << "Insert Error" << mysql_error(conn) << endl;
        cout << "Press B to go back";
        cin >> dummy;
    }
}else{
    cout << "Connection Error" << endl;
    cout << "Press B to go back";
    cin >> dummy;
}

system("pause");
system("cls");
}

实际输出:

输入编号:1 嘿,这是一个测试帖子//不可编辑 按 b 退出


预期结果:

输入编号:1 嘿,这是一个新帖子//可编辑

【问题讨论】:

  • C++ 中没有任何东西可以重写已经写出的输出。为此,您需要一个像 ncurses 这样的库。此外,您对 strBuff[] 的使用都是错误的,导致代码中出现未定义的行为
  • 只是在检查我可以使其工作的不同方法,因此使用了 strBuff [],正如你所说我应该使用 ncurses,我可以使用它的代码块吗?我需要导入库吗?你能分享更多的见解吗?

标签: c++ command-line-interface


【解决方案1】:

您似乎正在尝试制作某种交互式应用程序,允许用户编辑表中的值。这不能通过更改已经写入输出的内容来实现。相反,您可以提示用户输入来自cin 的新值(就像按下“B”按钮一样)。然后您可以将更新的值存储在表中。像这样的东西(未经测试的代码):

cout << "current value: " << strBuff[1000] << endl;
cout << "Enter new value (ENTER to keep value): " << flush;
string newvalue;
cin >> newvalue;
if ( newvalue.size() > 0 ) {
  stringstream update;
  update << "UPDATE se__dbms SET content = " << newvalue << " WHERE id = '" << id << "'";
  // execute the SQL statement
  ...
}

如果您需要看起来更像表格编辑器的东西(用户可以在适当的位置编辑数据),那么您将不得不使用像 ncurses 这样的东西,它允许您在控制台中的某些位置读取字符,甚至是某种图形用户界面。

【讨论】:

  • 是的,我的问题的解决方案是完全覆盖现有的单元格,如果没有成功,这是我的最后一个选择。我认为这个问题可能还有其他解决方法;正如你所说我可以使用ncurses,这可以使用代码块吗?我可以通过仅包含 ncurses 库来使其工作吗?
  • @SohailAnsari 你不能“只包含库”——你也必须实际编写代码。计算机不知道你想要可编辑的文本只是因为你包含了 ncurses。
猜你喜欢
  • 2014-05-09
  • 2021-10-24
  • 2014-12-16
  • 2020-10-12
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
相关资源
最近更新 更多