I have a project where I have to read from a text file and encode its contents using a Book Cipher. The first step I assume would be reading from two text files and count the page/line/column numbers(page numbers are determined by the delimiter '\f' and lines are determined by '\n'). However, I do not want the user to have to rename their file to "message.txt" everytime for the file to be read. Is there anyway for a C++ program to read any text files inputed by the user?
My current test program code:
#include <iostream> // Basic I/O
#include <string> // string classes
#include <fstream> // file stream classes
#include <sstream> // string stream classes
#include <locale> // locale class
using namespace std;
// version 0
int main() {
while (!cin.eof()) {
char ch;
cin >> ch;
cout << ch;
}
}
It gets everything from the file and outputs it correctly, however this way I can only accept one input file. I was hoping I can do something like this:
int main( int argc, char* argv[])
so:
argv[0] = program name
argv[1] = book cipher
argv[2] = message file
argv[3] = output file (encoded message goes here)
Is there a C++ version of this? I tried ifstream read(argv[1]), which did not work as I expected and it threw an exception. I feel like char* argv[] is something that is used in C, but not C++ as C++ has a string and strings are not a char array like in C. Wondering if someone could write me a sample just to see how the syntax work for this in C++.
And what which input function can I use to count escape characters such as '\n' and '\f'? I know from Java, I could use charAt() and do it in a loop or in C, it is just a char array. Input is really confusing for me in C/C++, I will keep looking at looking at tutorials on how these different functions work. Any ideas will be appreciated.