executing a program and arguments from the command line

So, i have an assignment and I'm trying to get the program to execute, read and write to files from the command line in cmd.

I can execute the file, but I can't get a file to open from argv[1]...

Any help (and an explanation for future studies) would be very much appreciated...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int main (int argc, char* argv[])
{
    char inputFileName[256];
    if (argc <= 1)
    {
       cout << "Enter name of file to copy" << endl;
       cin.getline(inputFileName, sizeof(inputFileName));
       }
    else
    {
       char  *inputFileName = argv[1];
       }    
    
   ifstream fin( inputFileName, ifstream::binary);
   if ( !fin ) {
      cerr << "File " << inputFileName << " not found!";
      getch();
      return 1;
   }//endif

   char *outputFileName;
   if (argc <= 2)
    {
       cout << "Enter name of definition file: ";
        cin.getline( outputFileName, sizeof( outputFileName ) );
       }
    else
    {
       char *outputFileName = argv[2];
        }
        
   ifstream ftest( outputFileName );
   if ( ftest ) {

      cout << "File " << outputFileName << " already exists."<< endl;
      cout << "Do you want to overwrite this file? (Y|N)" << endl;
      char response = toupper(getch());
      if ( response != 'Y' ) {
         cout << "Ending program." << endl;
         getch();
         return 2;
         }
      ftest.close();
      }
      ofstream fout( outputFileName );
      
      if (Encryptor(fin, fout))
      {
         cout << "Encryption successfull.\n";
         }
      else 
      {
           cout << "File could not be encrypted. Check file contents.\n";
           }
           
           cin.get();
           return 0;
      }
Last edited on
What are the errors? Also, use [code] tags and indent please.
I get

"File <random garbage> not found!"
Line 11: This line shouldn't be compiling >_> Anyway, it doesn't do what you think it does.
What does it do and how do I get inputFileName to point to the char address stored in argv[1]?
Last edited on
If I had to guess...it is creating a different temporary variable called inputFileName and assigning something to it. You aren't assigning it to your "real" inputFileName.
When I take out the char at the front of *inputFileName, it doesn't compile ans says "Cannot convert 'char*' to 'char'"

Then if I put the dereferencer back and then deference the argv[1], i get the same 'file <garbage> not found.' when I compile.
Figured it out.
Topic archived. No new replies allowed.