单击按钮即可将计数添加到Qt框架



我有一个按钮,可以将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";

我现在拥有的文件:
FirstName:
LastName:
年龄:

姓氏:年龄:

所需输出:

1号学生
姓氏:
年龄:

2号学生
姓氏:

我希望每次点击保存按钮时N°(var(为+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;
}

最新更新