Search code examples
c++databasefileifstream

C++ writing a string to a line in a text file; New line issue \n not working


I am writing a database program with many features (Read, write, delete, search, login ect. ) and my writing feature just stopped working (It was working 3 days ago) and I have no idea what changed. My writing function (void savescore) is supposed to write my input (cin username and password) and then move to the next line so I can input some more info the next time I decide to go and write to the file. Right now it's just writing over what I last put in.

test2.txt - Username, Password

Then I go to edit and enter "User, Pass" and this is what happens

test2.txt - User, Pass

I want it to enter that on the next line and I did "\n" Can someone give me some help? Thanks

CODE:

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <string>
#include <math.h>

using namespace std;

// Variables
string username;
string password;

//alphabet order functions

// Functions
void SaveScore()
{
  ofstream Database;
Database.open("test2.txt");
Database << username << " " << password << "\n";


Database.seekp(0,std::ios::end); //to ensure the put pointer is at the end
Database.close();
}

int main()
{

    int db;

    char ans;

    string save;

    string file;

    ifstream fin;
    ofstream fout;
    string searchpar;

    char repeat;
    bool loop = true;
    while (loop == true)
    {

        cout << "WELCOME TO MY DATABASE\n\n";
        cout << "To view the database, press 1\nTo edit the database, press 2\nTo search the database, press 3\nTo log in, press 4\n";
        cin >> db;
        system("CLS");

        if (db == 1)
        {
            cout << "Here is the database: \n\n";

            string line;

            ifstream myfile("test2.txt");
            if (myfile.is_open())
            {
                while (getline(myfile, line))
                {
                    cout << line << '\n';

                }

            }

            //open while bracket
            cout << "\n\nWould you like to return to the menu(y/n)?";
            cin >> repeat;

            if (repeat == 'y')
            {
                loop = true;
            }

            else if (repeat == 'n')
            {
                loop = false;
            }
            system("CLS");

        }

        else if (db == 2)
        {

            cout << "Please enter your username : ";
            cin >> username;

            cout << "\nPlease enter your password: ";
            cin >> password;

            SaveScore();

            cout << "\n\nWould you like to return to the menu(y/n)?";
            cin >> repeat;

            if (repeat == 'y')
            {
                loop = true;
            }

            else if (repeat == 'n')
            {
                loop = false;
            }
            system("CLS");

        }
    }

}

Solution

  • You say your program is

    replacing the first line of the text file everytime I try to write something new into it

    As it turns out, that's exactly what you have asked it to do. Consider:

    Database << username << " " << password << "\n";
    Database.seekp(0,std::ios::end); //to ensure the put pointer is at the end
    

    You are opening the file (when the write pointer starts at the start of the file, writing some data, and then seeking to the end. Seeking to the end does not change the fact that you've already written the text. Swap the order of the above lines to get what you want.

    Alternatively, you can open the file in "append" mode using:

    Database.open("test2.txt", std::ios::app);
    

    In this situation, you can omit the call to seekp entirely, since all data will automatically be written to the end of the file. See http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream for full documentation on this.