0

I have a button that appends from lineEdit to a *.txt file. I want to know if there is something i can do to get something like this :

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

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

I want that everytime my program checks last student number (the one inserted before) and adds +1 to the one i'm trying to append.

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

The File I have Now :
FirstName :
LastName :
Age :

FirstName :
LastName :
Age :

Desired Output :

Student N°1
FirstName :
LastName :
Age :

Student N°2
FirstName :
LastName :
Age :

I want that N°(var) to be +1 everytime i hit save button

  • Please clarify your question: can you give an exact example of a before file and after file? – user2205930 May 26 '21 at 16:56
  • Old : FirstName : LastName : Age : FirstName : LastName : Age : New : Student N°1 FirstName : LastName : Age : Student N°2 FirstName : LastName : Age : I want that N°(*) to be +1 everytime I hit save button (append to my txt file) –  May 26 '21 at 17:03
  • @user2205930 I edited my post , can you check please –  May 26 '21 at 17:05
  • maybe this can help you https://stackoverflow.com/questions/67573069/modify-and-append-extra-lines-in-a-file/ – NaturalDemon May 26 '21 at 17:36
  • Can you just put a counter in your class and increment it every time you enter your code you listed? – Joseph Larson May 26 '21 at 18:35
  • Are you forced to use this format or can you use XML, JSON or CSV instead? By using a more standard format you could add fields, such as `count`, which you can easily increment on each save. I.e. if using XML you can use xpath to easily increment numbers. – user2205930 May 26 '21 at 19:03
  • is the counter value determined by the content of the file or by the number of button presses? – Thomas May 26 '21 at 21:02

1 Answers1

1

this might help you along. now you only need to make a method / function to update a specific "student" datatype and write back to file.

#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;
}
NaturalDemon
  • 934
  • 1
  • 9
  • 21