Ошибка при запуске моей функции num_of_lines. Мне нужно открыть и закрыть файл внутри функции, и вам придется сделать то же самое для пары других функций. Это значит, что я могу получить информацию от попрошайки файла (я не могу использовать какие-либо другие функции, которые позволили бы мне сделать иначе, потому что мы еще не изучили их в классе). Я получаю две ошибки:Пытается открыть и закрыть файл внутри функции
hello.cpp:14:17: error: use of undeclared identifier 'file'; did you mean
'fill'?
ilines.open(file); //does file need to be a c-string
^~~~
fill
hello.cpp:14:12: error: no matching member function for call to 'open'
ilines.open(file); //does file need to be a c-string
Не уверен, что делать, спасибо.
#include <iostream>
#include <fstream>
#include <cctype>
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
int num_of_lines(string name)
{
int cnt3 = 0;
string line;
ifstream ilines; //Do I need to check for successful opening and closing each time
ilines.open(file); //does file need to be a c-string
if(ilines.is_open())
{
while(getline(ilines, line))cnt3++;
}
ilines.close();
return(cnt3);
}
int main(int argc, char **argv)
{
int num_of_lines(string name);
string file;
file = argv[1];
if(argc == 1)die("usage: mywc your_file");
ifstream ifs;
ifs.open(file);
if(ifs.is_open())
{
ifs.close();
int a;
a = num_of_lines(file);
cout <<"Lines: " << a << endl;
}
else
{
cerr <<"Could not open: " << file << endl;
exit(1);
}
//ifs.close();
return(0);
}
Ваш параметр называется 'name'. – immibis
... И да, это должна быть строка стиля C, которую вы можете получить таким образом: 'name.c_str()' – Beta
Я изменил ifs.open (файл) на ifs.open (name.c_str()) и Я получаю ошибку сегментации: 11 –