My question is how can i save the information & use again after exiting the program and Run it again.In other words, How can i save my info like that i can use this when i required.
For example,i make a program of save the info about the students,like their name,father name and class or roll No. I want that once i had put this info in program i can be used again.
And how can i add new students and also saving their records.Thanks to all in advance
If u give me an example i will better understand this. . . You can give me example a program that just input user name and store it.user can enter a new name when required. thanks
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::cout << "Enter a filename: ";
std::string fname;
if(std::getline(std::cin, fname))
{
if(std::ifstream file {fname}) //if we can read the file (e.g. it already exists)
{
std::string line;
if(std::getline(file, line)) //if we can get a line from the file
{
std::cout << "The first line of \"" << fname << "\" is \"" << line << "\"." << std::endl;
}
else
{
std::cout << "The file \"" << fname << "\" is empty." << std::endl;
}
}
elseif(std::ofstream file {fname}) //otherwise, try to write to the file
{
std::string text;
if((std::cout << "Enter some text to write to the file: ") && std::getline(std::cin, text))
{
if(file << text << std::endl) //if write was successful
{
std::cout << "Your text was written to the file." << std::endl;
}
}
}
else
{
std::cout << "Could not read or write the file \"" << fname << "\" - is it valid?" << std::endl;
}
}
}