Я пытаюсь открыть файл из аргумента командной строки.Открытие текстового файла из аргумента командной строки
Я отлажена своя программа:
при печати значения файла он дает значение < неполному тип>.
Когда я печатаю значение argv, оно дает значение (char **) 0x7fffffffe4e0.
Когда я печатаю значение argv [1], оно дает значение 0x0.
Функция не откроет мой файл. Не знаете почему?
Я пробовал:
if (file.is_open())
тоже и та же проблема.
В моей основной функции я прохожу:
buildBST(&argv[1]);
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 words
}
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;
}
Если 'argv [1]' is 'NULL', то проблема заключается в том, что вы не передаете имя файла в качестве первого аргумента. Как вы запускаете программу? – Rufflewind
Скомпилировать с помощью 'g ++ -g -std = C++ 11 myfile.cpp' и выполнить './a.out test.txt' – dave1234
Подождите, вы передали '& argv [1]' 'buildBST', так что это означает параметр 'argv' в' buildBST' теперь указывает на первый аргумент *. Это означает, что 'argv [1]' inside 'buildBST' ссылается на * второй аргумент *, а не на первый! (Помните, что индексы в C равны * на основе нуля.) – Rufflewind