Opening a text file from a command line argument

I am trying to open a file from a command line argument.
I've debugged my program:
when I print the value of file it gives the value <incomplete type>.
when I print the value of argv it gives the value (char **) 0x7fffffffe4e0.
when I print the value of argv[1] it gives the value 0x0.

The function won't open my file. Not sure why?
Ive tried:
if (file.is_open()) too and same problem.

In my main function I pass:
buildBST(&argv[1]);

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
  BSTTreeData buildBST (char *argv[]){
    vector <string> allwords;

    BSTTreeData Data;
    BinarySearchTree<string> Tree;

    ifstream file (argv[1]);

    char token;

    string listofchars = "";
    string input;

    int distinct = 0;
    int line = 1;

    if (file){
        //if the file opens
        while (getline(file, input)) //gets every line in the file
        {
            for (int i = 0; i < input.size(); ++i) //gets all the contents in the line
            {
                token = input[i]; //each character become a 'token'
                if (isalpha(token))
                {
                    //if the character is an alphabetical character
                    listofchars += token; //append character to a string
                    if (Contains(allwords, listofchars) == false)
                    {
                        //if the current word has not already been added to vector of words
                        //increment the distinct word count
                        distinct += 1;
                        Tree.insert(listofchars); //creates the BST
                        allwords.push_back(listofchars); //add current word to vector of all the w\
ords
                    }
                    else
                    line++; //increments the line number
                }
                else
                    line++; //increments the line number
            }
            listofchars = ""; //creates empty character string
        }
        file.close(); //closes file

        Data.BST = Tree;
        Data.linenumber = line;
        Data.distinctwords = distinct;
        Data.words = allwords;
        return Data;
    }
}
ifstream file (argv[1]);
Think about how many elements are in the array that you are passing.
Topic archived. No new replies allowed.