why there are garbage characters after running the code?

I tried to run the code (saved as 1.cpp, see at the end) in my linux:
I input in the terminal:
g++ 1.cpp
Then
./a.out hello.txt

I thought it would output the file of hello.txt. However, the result is that it indeed outputs the file. But before the content of file, there are garbage characters. I just don't know why. Any kind of help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(int argc, char** argv){
	int fail_count = 0;
	for(int i = 0; i < argc; ++i){
		ifstream in(argv[i]);
		if(in){
			string s;
			while(getline(in, s))
				cout << s << endl;
		}else {
			cerr << "cannot open file " << argv[i] << endl;
			++ fail_count;
		}
	}
	return fail_count;
}
How many characters? Three?
@helios
A lot of characters. Then there are 62; 9; c.
Oh, now I see it.

argv[0] is the name of the executable. Your loop starts at i == 0, you open the executable on line 8, and you read it as if it was a text file on line 11.
Topic archived. No new replies allowed.