I'm new to programming and one of the projects I would like to tackle would be a quiz, the problem is I do not know how to store my quiz scores in a text file, can someone help plz? A given example would be nice
Asked
Active
Viewed 212 times
1 Answers
2
Here is slightly modified example of the one given in cplusplus
// basic file operations
#include <iostream>
#include <fstream>
int main () {
std::ofstream myfile("example.txt");
if(myfile.is_open())
{
myfile << "Writing this to a file.\n";
myfile << 1 << 2 << 3;
}
else
{
std::cout << "error opening file" << std::endl;
}
return 0;
}
This creates example.txt
in your working directory if it does not exist yet, opens it, streams to it and closes the file as myfile
is destroyed. Now the file contains the following text:
Writing this to a file.
123

StefanKssmr
- 1,196
- 9
- 16
-
You should also check if (myfile.is_open()) and then pass data there I guess – NixoN Jun 04 '20 at 11:03
-
1I updated my answer. Sorry for mixing cplusplus and cppreference. I am simply not focused enough today... – StefanKssmr Jun 04 '20 at 11:05
-
1