79786870

Date: 2025-10-09 21:48:54
Score: 0.5
Natty:
Report link

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".

Updated Demo

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: mini kids