Yes, this is homework help: I received some feedback from my professor, I just flat out don't understand what he is trying to say, and the frustrating part is he and every other example say it like it is the most simple and obvious thing in the world.
I am do the classic a.out "nulls" < somefilename.txt that is supposed to encrypt and decrypt from the command line USING ONLY CIN to get the file contents. I am using codeblocks, and trying to run from the "set program arguments" function that replicates the command line.
My request: Please post a very simple code the uses this < to get the data into the program and displays it
something like:
int textplaceholder ;
//proper code to make "<" from the command line open the file and load the text
cout << textplaceholder ;
return 0 ;
I just need to understand how that thing works, I have put at least 15 hours into this, I cannot go see my professor because I am traveling (this is an online class)
I got it to work just fine when I use it, but he is doing something differently and breaking it, so my work doesn't do what he was asking.
Thanks.
My code and feedback below:
1 /*-----------------------------------------------------
2 * Assignment_06.cpp
3 * max_02
4 * Mon 28 July 12:36:11 PDT 2014
5 /max02.tar.25.cli.parms.wrong.explicit.io.not.as.assigned
6 -----------------------------------------------------*/
7 /*
8 you use argv[1], argv[2], argv[4],and argv[6]. This effectively
9 renders the program unusable unless the user inserts random
10 words to properly space out the real control inputs.
11 */
12 #include <iostream>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <string>
16 #include <cstring>
17 #include <fstream>
18 #include <time.h>
19 using namespace std;
20 int main(int argc, char *argv[]) // put argv in main
21 {
22 /*
23 no need for explicit file i/o, could read cin.
24 */
25 ifstream file ;
26 file.open(argv[4]) ; //open file in argument ;
27 int nulldrop = atoi(argv[2]); // initiate and get null count
28 char original ; // stream place holder for encrypted text
29 string decryption ;// initiate stream for decryption
30 int nullc = 0 ;// initiate counter
31 if (strcmp(argv[1], "-d") == 0)
32 {
33 original = file.get() ;// get character
34 while (! file.eof())// while getting characters and not at end of file
35 {
36 if(nullc >= nulldrop)// if count = null
37 {
38 decryption += original ;// then add it to the decryption string
39 nullc = 0 ;// reset count to 0
40 original = file.get() ;// get the next character
41 }
42 else // else
43 {
44 nullc++ ;// count ++
45 original = file.get() ;
46 }
47 }
48 cout << decryption << '\n' ;// after EOF, output decrypted stream
49 }
50 else if (strcmp(argv[1], "-e") == 0)
51 {
52 string encryption ;
53 int en, ena ;
54 nullc = nulldrop ;
55 while (! file.eof())// while getting characters and not at end of file
56 {
57 if(nullc <= 0)// if count = null
58 {
59 original = file.get() ;// get the next character
60 encryption += original ;// then add it to the decryption string
61 nullc = nulldrop ;// reset count to nulls
62 }
63 else // else
64 {
65 int t1 = time(0) ;
66 /*
67 only run srand() one time at the start of the program.
68 by continually re-seeding the randomizer this way,
69 the resulting stream of pseudorandom numbers is much
70 less random
71 */
72 srand((t1-(int(original-en) - nullc))/1.4) ;
73 en = rand() % 25 + 97 ;
74 if (en > 112 && nullc > 1)
75 {
76 ena = en - 32 ;
77 }
78 else
79 {
80 ena = en ;
81 }
82 encryption += char(ena) ;
83 nullc-- ;// count --
84 }
85 }
86 ofstream ofile ;
87 ofile.open(argv[6]) ;
88 ofile << encryption ;
89 ofile.close() ;
90 }
91 return 0;
92 }
//proper code to make "<" from the command line open the file and load the text
There's no need to write code to open the file, the operating system will open it for you. All that is required is to use cin as though the user will be typing the data on the keyboard.
int main(int argc, char *argv[]) // put argv in main
{
//ifstream inFile ;
//string filename ;
char original ; // stream place holder for encrypted text
//inFile.open("filename") ; //open file in argument ;
int nulldrop = atoi(argv[2]); // initiate and get null count
string decryption ;// initiate stream for decryption
int nullc = 0 ;// initiate counter
if (strcmp(argv[1], "-d") == 0)
{
original = cin.get() ;// get character
while (! cin.eof() && int(original) != 10)// while getting characters and not at end of file
{
if(nullc >= nulldrop)// if count = null
{
decryption += original ;// then add it to the decryption string
nullc = 0 ;// reset count to 0
original = cin.get() ;// get the next character
}
else // else
{
nullc++ ;// count ++
original = cin.get() ;
}
}
cout << decryption << '\n' ;// after EOF, output decrypted stream
}
return 0 ;
}
and I want this program to get text from a file at the command line and display it by typing "stuffdisplay.exe < document.txt" at the command line, then this little sample above should work???
#include<iostream>
usingnamespace std;
int main()
{
char ch;
while (cin.get(ch))
cout << ch;
}
Assume the resulting program is called "prog1.exe", at the command line, type: prog1.exe < document.txt
and the contents of the file document.txt should be displayed.
Or a line at a time,
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string stuff;
while (getline(cin, stuff))
cout << stuff << '\n';
}
What I tend to do, to avoid re-typing the same thing into the command line over and over again, is to create a .bat file. That's just a text file containing commands you want to be executed.
In my example the file looked a bit like this:
1 2
prog1.exe < document.txt
pause
I saved that as "run.bat", put it in the same folder as both prog1.exe and document.txt. Then, double-clicking on run.bat opens a console window and executes the commands.
To begin with, I'd try it with just a simple "hello world" program which requires no input. Once that's working, move on to the program which requires the cin input.
(alternatively, you can put the run.bat in any convenient directory (or on the desktop) and enter the full path to the executable program (as well as the full path to the input file, if appropriate).
Thanks guys, it turned MinGW was the problem, none of the programs you gave me would work still. I switched over to TDM and everything works great now. Thanks agaon.