C++ 在按钮上向Qt框架添加计数单击
我有一个按钮,可以将lineEdit附加到*.txt文件。 我想知道我是否可以做些什么来得到这样的东西:C++ 在按钮上向Qt框架添加计数单击,c++,qt,C++,Qt,我有一个按钮,可以将lineEdit附加到*.txt文件。 我想知道我是否可以做些什么来得到这样的东西: Student N°1: FirstName : ----- LastName: ----- Age: ----- Student N°2: FirstName : ----- LastName: ----- Age: ----- 我希望每次我的程序检查最后一个学生编号(之前插入的编号)并将+1添加到我试图追加的编号上 QFilefile("***"); if (fi
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";
QFilefile(“***”);
if(file.open(QFile::Append)){
QTextStream out(文件(&F);
这可能对你有帮助。
现在,您只需要创建一个方法/函数来更新特定的“student”数据类型并写回文件
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
typedef std::向量;
体类型
{
学生():名(“”),姓(“”),年龄(0){};
学生(const std::string和firstname,const std::string和lastname,未签名的短年龄):firstname(firstname),lastname(lastname),age(age){};
std::stringfirstname;
std::字符串lastName;
无符号短龄;
};
typedef std::vector studentVector;
void readfile(字符串和文件名、studentVector和数组){
std::ifstream文件(文件名);
if(file.fail()){
//文件在此处不存在代码
std::请澄清您的问题:您能给出一个前后文件的确切示例吗?Old:FirstName:LastName:Age:FirstName:LastName:Age:LastName:Age:LastName:Age:学生1名字:LastName:Age:Student 2名字:LastName:Age:我希望每次点击保存按钮时N°(*)都是+1(附加到我的txt文件中)@user2205930我编辑了我的帖子,你能检查一下吗?这可能会有帮助吗?你能在你的类中放一个计数器,每次输入你列出的代码时递增它吗?