HALP with argc and argv[1]

argc is a int

argv[1] is a character pointer to array

how do i use this pointer to array!!?!
cout << "I want to use this array to print out all the numbers from my text file but i cant
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
#include <iostream>
#include <cmath>
#include <fstream>
#include<iomanip>
#include <cstdlib>

using namespace std;

//argv[1] is program name
int main(int argc, char*argv[])
{
	ifstream infile;
	ofstream outfile;
	
	if (argc>1)
	{
		infile.open(argv[1], ios::in);
	}
	if(infile.is_open())
	{
		outfile.open("Lab3Test.txt", ios::app);
	}
	else if(infile.fail())
	{
		ofstream outfile;
		outfile.open("Lab3Test.txt", ios::app);
		outfile << "failed to open." << endl;
		outfile.close();
	}
	
	//time to convert argv
	int x = atoi(argv[1]);
	int storage;
	
	while (!infile.eof())
	{
		storage += x;
	}
	storage / 30;
	
	
	
	cout << "The mean is " << storage;
}
cout << "I want to use this array to print out all the numbers from my text file but i cant
argv is an array of c-strings that contain the command line arguments given to your program. To print out all the numbers from your text file, you open your text file and read/print all of the numbers therein. argv doesn't play into it except for providing the file name specified via the command line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

int main(int argc, char*argv [])
{
    if (argc < 2) {
        std::cerr << "Please specify the file name when invoking this program.\n";
        return 0;
    }

    std::ifstream in(argv[1]);
    if (!in) {
        std::cerr << "Unable to open input file \"" << argv[1] << "\"\n";
        return 0;
    }

    int value;
    while (in >> value) 
        std::cout << value << '\n';
}
Topic archived. No new replies allowed.