An Addition to "https://stackoverflow.com/a/79786806/31654810", "fstream file(argv[1], ios::in | ios::app);" didn't work for me when I tried running it but you could open the file in just ios::in mode and when you want to append, you can create another fstream or ofstream in append mode to actually write it.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
void testFile(const char *fileName)
{
fstream file(fileName, ios::in);
string line, word;
while (getline(file, line))
{
istringstream checker_str(line);
if (checker_str >> word && word == "ret")
{
cout << "Message: " << "\033[92msuccess: \033[0m" << "\"ret\" keyword was already written." << endl;
return;
}
}
//cout << "Message: " << "\033[93mwarning: \033[0m" << "\"ret\" keyword was not written." << endl;
cout << "Message: " << "\033[93mwarning: \033[0m" << "\"ret\" keyword was not written and has been written for you." << endl;
ofstream file1(fileName, ios::app);
file1 << "\nret";
return;
}
int main(){
testFile("no_ret.txt");
testFile("Yes_Ret.txt");
return 0;
}
This would be the right code referenced/inspired from "https://stackoverflow.com/a/79786806/31654810".