【问题标题】:Adding Count to Qt Framework on buttonclick在按钮单击时将计数添加到 Qt 框架
【发布时间】:2021-05-26 16:44:25
【问题描述】:

我有一个从 lineEdit 附加到 *.txt 文件的按钮。 我想知道我是否可以做些什么来得到这样的东西:

Student N°1:
FirstName : -----
LastName: -----
Age: -----

Student N°2:
FirstName : -----
LastName: -----
Age: -----

我希望每次我的程序检查最后一个学生编号(之前插入的那个)并将 +1 添加到我尝试附加的那个。

QFilefile("***");
if (file.open(QFile::Append)) {
    QTextStream out(&file);
    out <<"Student Num:"<<"\n";
    out <<"Name:" << ui->lineEdit->text()<<"\n";

我现在拥有的文件:
名字 :
姓氏:
年龄:

名字:
姓氏:
年龄:

期望的输出:

学生 N°1
名字 :
姓氏:
年龄 :

学生 N°2
名字 :
姓氏:
年龄:

我希望每次点击保存按钮时 N°(var) 都为 +1

【问题讨论】:

  • 请澄清你的问题:你能给出一个前文件和后文件的确切例子吗?
  • 旧 : FirstName : LastName : Age : FirstName : LastName : Age : New : Student N°1 FirstName : LastName : Age : Student N°2 FirstName : LastName : Age : I want that N°( *) 每次点击保存按钮时都会 +1(附加到我的 txt 文件)
  • @user2205930 我编辑了我的帖子,你能检查一下吗
  • 你能在你的班级里放一个计数器,每次输入你列出的代码时递增吗?

标签: c++ qt


【解决方案1】:

这可能会对您有所帮助。 现在您只需要创建一个方法/函数来更新特定的“学生”数据类型并写回文件。

#include <fstream>
#include <sstream>
#include <vector>
#include <string>

#include <iostream>

#include <ctime>


using namespace std;

typedef std::vector<string> Vector;

struct Student
{
  Student () : firstName(""), lastName(""), age(0){};
  Student (const std::string& firstname, const std::string& lastname, unsigned short age) : firstName(firstname), lastName(lastname), age(age){};

  std::string firstName;
  std::string lastName;
  unsigned short age;
};


typedef std::vector<Student> studentVector;


void readReadFile(string &fileName, studentVector &array){
    std::ifstream file(fileName);
    if(file.fail()){
            //File does not exist code here
            std::cout << "File doesn't exist." << endl;
            return;
        }
    else{

        int counter = 0;
        std::string str;

        string studentName = "";
        string studentLastName = "";
        int age = 0;

        while (std::getline(file, str)) {
            if(counter == 0){
                //++counter;
                std::string strName="FirstName :";
                std::string str2 = strName.substr (0,11);
                //std::cout << "FirstName : " << str2 << endl;
                if(str.length() < 11){
                    return;  // no name present
                }
                std::string::size_type posName = str.find(str2);
                //std::cout << "size_t: " << posName << endl;
                if (posName != string::npos) {
                    //.. found.
                    std::string str3 = str.substr (12, str.length());
                    std::cout << "Name: " << str3 << endl;
                    //std::cout << "size_t: " << posName << endl;
                    std::cout << "found Name" << endl;
                    studentName = str3;
                }
            }
            if(counter == 1){
                //++counter;
                std::string strName="LastName :";
                std::string str2 = strName.substr (0,10);
                //std::cout << "LastName: " << str2 << "length: " << str2.length() << endl;
                if(str.length() < 10){
                    return;  // no lastname present
                }
                std::string::size_type posLastName = str.find(str2);
                //std::cout << "size_t: " << posName << endl;
                if (posLastName != string::npos) {
                    //.. found.
                    std::string str3 = str.substr (11, str.length());
                    std::cout << "LastName: " << str3 << endl;
                    //std::cout << "posLastName : " << posLastName  << endl;
                    std::cout << "found lastName" << endl;
                    studentLastName = str3;
                }
            }
            if(counter == 2){
                //++counter;
                std::string strName="Age :";
                std::string str2 = strName.substr (0,5);
                //std::cout << "Age: " << str2 << endl;
                if(str.length() < 5){
                    return;  // no age present
                }
                std::string::size_type posAge = str.find(str2);
                //std::cout << "size_t: " << posName << endl;
                if (posAge != string::npos) {
                    //.. found.
                    std::string str3 = str.substr (6, str.length());
                    std::cout << "Age: " << str3 << endl;
                    //std::cout << "posAge : " << posAge  << endl;
                    std::cout << "found age" << endl;
                    age = std::stoi(str3);
                }
            }
            ++counter;

            std::string::size_type posName = str.find("---");
            if (posName != string::npos) {
                counter = 0;
                Student test(studentName, studentLastName, age);
                array.push_back(test);
                std::cout << "------------------" << endl;
                // std::cout << "empty" << std::endl; // white line
            }
        }
        file.close();
    }

}

void appendstuff(Student data, studentVector &array){
    array.push_back(data);
}

void write_students_backtofile(string &fileName, studentVector &array){
    // https://stackoverflow.com/questions/17032970/clear-data-inside-text-file-in-c
    std::ofstream myfile;
    myfile.open(fileName, std::ofstream::out | std::ofstream::trunc);
    //myfile.close();

    // write vector back to file
    //ofstream myfile;
    //myfile.open (fileName);

    for(auto& i : array){
        myfile << "FirstName : " << i.lastName << "\n";
        myfile << "LastName : " << i.firstName << "\n";
        myfile << "Age : " << i.age << "\n";
        //myfile << "\n";  // white line
        myfile << "---\n";  // white line
    }
    myfile.close();
    array.clear();
}

int main(){
    std::clock_t start;
    double duration;
    start = std::clock();

    studentVector array;

    string fileName = "input.txt";
    readReadFile(fileName, array);

    Student test2("Frodo", "Baggins", 66);
    Student test3("Gandalf", "the Grey", 254);
    Student test4("Saruman", "the White", 450);

    appendstuff(test2, array);
    appendstuff(test3, array);
    appendstuff(test4, array);


    std::cout << "----------------------------------" << endl;
    for(auto& i : array){
        std::cout << i.lastName << " " << i.firstName << " " << i.age << endl;
    }



    string testFile = "test.txt"; // for testing.
    write_students_backtofile(fileName, array);



    duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
    std::cout<<"printf: "<< duration << " seconds" << '\n';

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2012-01-24
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多