Simple Win32 question *argv[]

My teacher asked us to create a win32 command line text editor. I have completed the program and it works great. Then he asked us to accept two command line arguments to our program (EX: textedit -f commandts.txt). If the program is started without any arguments then cin is used to get input from keyboard. If the -f commands.txt arguments are used the program should read the input from the file instead of the keyboard. I cannot figure out how to do this. My main looks like this so far:

1
2
3
4
int main(int argc, char *argv[])
{
...
}


Could someone direct me in the right direction?

Thanks
argc has the number of commandline arguments and argv has the actual arguments. The arguments are separated by a space (?or another delimeter? usually just a space).

It's actually quite easy. Assume you run the program with "textedit -f commands.txt"... you'd have the following:

1
2
3
4
argc == 3
argv[0] == "textedit"
argv[1] == "-f"
argv[2] == "commands.txt"


As for getting input from a file vs. cin... remember that both ifstream object and cin are 'istream' objects... so...
1
2
3
4
5
6
7
8
9
10
11
12
void DoSomething(istream& stream)
{
  stream >> foobar;
}

int main()
{
  DoSomething( cin );  // read from cin

  ifstream file( "filename.txt" );
  DoSomething( file );  // read from file
}
Maybe I'm just missing something...

Let's say I had the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main (int argc, char *argv[])
{
    string myInput, iFName;
    ifstream iFile;
    if (argc > 0)
    {
         if (argv[1] == "-f")
        {
                iFName = argv[2];
                iFile.open(iFName);
        }
    }
//CONFUSED WHAT TO DO FROM HERE TO MAKE THE FOLLOWING LINE READ FROM FILE INSTEAD OF KEYBOARD WITHOUT CHANGING IT???
    getline(cin, myInput);
}
You can use getline() with files, so just replace "cin" with "iFile".
The problem is, my program has 300 lines and all the inputs have been programmed to use cin. The program has to work with either the keyboard OR a file as input. Our teacher did an example in class of how to accomplish this without having to change every cin but I can't remember how he did it.

I know I can do: textedit <commands.txt and that will accomplish what I want but I have to make it work using: textedit -f commands.txt.

Thanks for the help so far!
You have to go in and change all of your code so that it doesn't use cin. See my earlier post. The only shortcut I can think of is to redefine a new istream variable called 'cin' in the functions that use it -- but that's "very very bad (tm)"

You have just learned the hard lesson of why abstraction is so important. If you use cin directly, then your code always uses cin. If you use an abstract istream reference, then your code will work with any type of istream.
Ok thanks for the help. That's probably what he did in class but I just can't remember. I shall begin the long task of changing my code.
One more question? How could I accomplish the following partial-sudo code in reall c++:

1
2
3
4
5
6
7
8
9
10
11
//If (user enters command line arguments)
{
//ifstream equals myIStream;
}
else
{
//cin equals myIStream;
}

cout << "Enter something: ";
myIStream >> something; //where myIStream is either got from cin or ifstream??? 
See my original post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void GetDataFromStream( istream& stream )
{
  // read from whatever stream was passed
  stream >> foo;
}

int main()
{
  if( read_from_file )
  {
    ifstream myIStream( "your file.txt" );
    GetDataFromStream( myIStream );
  }
  else
    GetDataFromStream( cin );
}


If you can't create a new function for some weird reason, you can use a pointer. This works but is less preferable (the below code will open the file even if the user wants to use cin):

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  istream* ptr;
  ifstream myIStream( "your file.txt" );

  if( read_from_file )
    ptr = &myIStream;
  else
    ptr = &cin;

  cout << "enter something: ";
  *ptr >> something;
}
Topic archived. No new replies allowed.