0

I need to open a specified directory entered by user, open every files of that folder directory, count the number of line of each file and then finally to do a total sum of all the lines of all files in that folder. So basically I succeed to open the directory and put all the file names in a vector array. So I did:

vector < string > dirlist;

dirlist.push_back(ent->d_name) //To add file names in the vector array

The names was successfully push in the vector array and I was able to display it.

But now I want to use a for_each to apply lineCount function to each element of the vector array I can’t:

for_each (dirlist.begin(),dirlist.end(), lineCount);

lineCount function:

void lineCount (string fn)
{ 
    ifstream fileInput;
    string line;
    int number_of_lines = 0;  //initialise number_of_lines to 0
    fileInput.open(fn.c_str(), std::fstream::in); //open file
    if(fileInput.is_open()) {  //test if file is opened
            while(getline(fileInput, line)) {
                ++number_of_lines;
            }
            cout << "Number of lines in file: " << number_of_lines;
            fileInput.close();
    }
    else cout <<"Unable to Open File"<<endl;

}

Now I get Unable to open for every files in the directory

13
  • 1
    What do you need this for? You can achieve this using your favourite unix shell.
    – Bathsheba
    Jun 16, 2016 at 7:00
  • 1
    Since you're only reading from the file, you don't need to use | std::fstream::out - and in fact if you do, you make get a privilege error. Jun 16, 2016 at 7:07
  • 2
    What do you mean by "I can't"?
    – melpomene
    Jun 16, 2016 at 7:08
  • 3
    We don't know what your problem is: Does it give a compiler error? Does it give one of your error messages? Does it simply do nothing? Jun 16, 2016 at 7:11
  • 1
    @ASG What do you mean by "I can't"?
    – melpomene
    Jun 16, 2016 at 7:15

1 Answer 1

0

The line

fileInput.open(fn, std::fstream::in | std::fstream::out); //open file

has two problems:

  1. The first parameter to open() expects a const char * - you're giving it a string.
    Use fn.c_str()
  2. The second parameter says to open the file for both in and out - but you're only going to be reading it.
    Remove | std::fstream::out.

And don't forget: all that you are passing to the lineCount() function is the name of the file: the actual directory still needs to be prepended to the name of the file. I don't know what you call it - or if the directory has a path separator ('\' or '/') at the end of it - but you basically need to do this:

fn = dir + fn;

before attempting to open the file.

3
  • ...Thank you very much. It worked but the problem now it that the else statement is executed...I don`t get the number of files but I get "The File Name" Unable to Open File when it should open the file, count the line number then display it.
    – user6473027
    Jun 16, 2016 at 7:39
  • I get Unable to open files for every files in the folder
    – user6473027
    Jun 16, 2016 at 7:44
  • @ASG I've updated my answer: please have a look. You really need to use a debugger, and look at the values that are being used. This should help you understand what is actually happening Jun 16, 2016 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.