-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_data5.cpp
More file actions
48 lines (44 loc) · 1.19 KB
/
Copy pathread_data5.cpp
File metadata and controls
48 lines (44 loc) · 1.19 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
/*
* It will iterate through all the lines in file and
* call the given callback on each line.
*/
bool iterateFile(std::string fileName, std::function<void (const std::string & )> callback)
{
// Open the File
std::ifstream in(fileName.c_str());
// Check if object is valid
if(!in)
{
std::cerr << "Cannot open the File : "<<fileName<<std::endl;
return false;
}
std::string str;
// Read the next line from File untill it reaches the end.
while (std::getline(in, str))
{
// Call the given callback
callback(str);
}
//Close The File
in.close();
return true;
}
int main()
{
std::vector<std::string> vecOfStr;
//Call given lambda function for each line in file
bool res = iterateFile("example.csv", [&](const std::string & str){
// Add to vector
vecOfStr.push_back(str);
});
if(res)
{
for(std::string & line : vecOfStr)
std::cout<<line<<std::endl;
}
}