0

My task is to make a C++ program that will read, write, save, load and append a text file. I have two issues that I've been stuck on so far. The first being, how do you store the first argument entered by a user in a string using argv? Secondly, how do I create the program such that when the user enters in the command the program doesn't exit immediately after, so technically be in a while loop the whole time until prompted by a quit message? I've tried doing this already but my code also goes into a loop.

int main(int argc, char* argv[]) {
   while (!inFile.eof()) {
   inFile.open("userinput.txt");
        getline(cin, line);

        if (argc > 1) {
            int result = strcmp(argv[1], "load");
            if (result == 0) {
                cout << "CORRECT" << endl;
                }
            else{
                exit(1);
                }
        }
    }
    return 0;
}
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
  • 4
    It looks like you're confusing the program arguments, `argv`, with standard input. – molbdnilo Aug 24 '20 at 09:36
  • You also need to declare `inFile` before you use it and open the file before you use it too. – Ted Lyngmo Aug 24 '20 at 09:37
  • `argv` is set once, at the start of your program. It will never change (unless your code modifies it, but that's dangerous). If you want to read something more you need to use standard input. – Yksisarvinen Aug 24 '20 at 09:37
  • 3
    First thing first, read [this](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – n. m. could be an AI Aug 24 '20 at 09:37
  • Command line arguments are ready only once when the program is started, but I suggest you can use scanf or something as specified by @n.'pronouns'm. and stay in while loop. – narengs7 Aug 24 '20 at 10:01
  • A String will be store in particular index of argv, which points to the starting location of the string you sent from command line. Later use a variable to refer. validate your while with the variable then you can use scanf to read again if command line string is invalid. so the same variable can be reutilised in while conditions. Hope it is what you are specifying in question. – narengs7 Aug 24 '20 at 10:02

1 Answers1

0

Something like this, read program argument, read user input, read/write/append on a file.

#include <iostream>
#include <ios>            // new
#include <fstream>        // new

using namespace std;

int main(int argc, char* argv[]) 
{
    fstream inFile("userinput.txt", std::ios_base::app | std::ios_base::out); //new, allows to append lines to 'userinput.txt'
    while (!inFile.eof()) {
        string line;
        getline(cin, line);
        inFile << line;  // new: write the user input on inFile

        if (argc > 1) {
            int result = strcmp(argv[1], "load");
            if (result == 0) {
                cout << "CORRECT" << endl;
            }
            else {
                exit(1);
            }
        }
    }

return 0;
}

I don't really know the usage of this though so you should adapt it to your ends.

Giogre
  • 1,444
  • 7
  • 19