//Reading the last name from the file
        ifstream file;
        file.open("studentName.txt");
        char name[20] ;
        file>>name;
        cout<<"The last user was: "<<name<<"\n";

        file.close();


        //Replace the last student name with the new one in the text file
        cout<<"What is your name?: ";
        cin>>name;
        name[0]=toupper(name[0]);
        ofstream file2;
        file2.open("studentName.txt");
        file2<<name;
        file2.close();

When I run the program I don't see anything in studentName.txt What's even more weird is that I can run the program without the txt file and it will read somewhere the name of the last user?

Are you sure that you are placing the studentName.txt file in the same folder as the .exe file that is generated by the compiler? I suspect that the file you think it's using is not the one that it is actually using. When you load "studentName.txt", it will open the studentName.txt file in the current directory of the running program (the .exe file), or where the IDE runs the program from. Most IDEs (like Visual Studio or CodeBlocks) have special folders within your project's top-level folder where it generates the executable and runs it from, and it may not be the one you think. Make sure to locate the .exe file, and place the studentName.txt file in that directory.

Here is one way of doing it:

#include  <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{

        ifstream file;    
        vector<string> theNames;
        string name,replaceName;

        file.open("studentName.txt",ios::in);

        //check to see if the file loaded
        if(!file.is_open())
        {
            cout <<"Cant find file\n";
            return 1;
        }

        while(!file.eof())
        {
            file >> name;
            //save all of the elements into a vector
            theNames.push_back(name);
        }

        cout<<"The last user was: "<<name<<"\n";      
        file.close();

        //Replace the last student name with the new one in the text file
        cout<<"What is your name?: ";
        cin>>replaceName;
        replaceName[0]=toupper(replaceName[0]);

        //delete the last name
        theNames.pop_back();
        //make an iterator to the end of the vector
        vector<string>::iterator itr = theNames.end();
        --itr; // move the iterator back one position
        //insert the replacement name 
        theNames.insert(itr,replaceName);


        ofstream file2;
        file2.open("studentName.txt");

        if(!file2.is_open())
        {
            //error
            return 1;
        }

        for(int i = 0; i < theNames.size()-1; i++)
        {
            file2<<theNames[i] <<endl;
        }


        file2.close();

 return 0;
}

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.