How to print all the contents of a file without eof(); or vectors or classes.

Pages: 12
they are command line arguments. That is, you can type your program's names followed by data, and that data is captured for the program to use, and in windows, this also allows the drag and drop interface to be accessed.
argc is a integer of how many things were passed in. the first location (from 0, so [0]), for mostly historical reasons, has the name of the program in it.
The other one is the data, as C-style strings.

so a simple program, then:
1
2
3
4
5
int main(int argc, char **argv)
{
	for(int i = 1; i < argc; i++)
	 cout << argv[i] << endl;
}


build that and run it from the command line with like
a.exe 1 2 3 4
and it will print
1
2
3
4
(where 1 2 3 4 are *strings* not integers -- they are much like using getline when you read typed in data).
-------------
you can read directly into integers:
int i;
cin >> i; //the >> operator does the stoi for you, behind the scenes

the same works on a file:
file >> i; //reads text from the file into the int doing the conversion for you

when preventing user goofs like typing strings when int is required you may prefer to read as string and convert yourself, though, to harden your program against errors.

yes, you can do logic on integer variables.
if (i < 43)
etc..
you can do them on strings too (but not C style strings):
if ( data > "aardvark")
Last edited on
Topic archived. No new replies allowed.
Pages: 12