code now
CODE
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
map < string, vector <int> > oNetDevMap;
class LineData
{
public:
const std::string& getName() const {return(name);}
int getData(int index) const {return(data[index]);}
int getDataLength() const {return (data.size());}
void setName(string ivalue) {name = ivalue;}
void setData(vector <int> ivalue) { data = ivalue;}
private:
std::string name;
std::vector<int> data;
};
vector <LineData> oNetDevList;
int main()
{
ofstream test;
test.open("test.txt");
vector<vector<int> > numsInRows; // vector of vectors emulates a 2D array
string throwAway; // for the text we don't need
ifstream file("/proc/net/dev"); // try and open the file
if(file.is_open()) // if it's open
{
getline(file, throwAway); // read the first line containing the headers
getline(file, throwAway); // read the first line containing the headers
while(file.good()) // while we haven't hit eof or errored
{
string line;
string line2;
string device;
getline(file, line); // get the rest on the line into the string
line2 = line.substr(line.find(":")+1);
device = line.substr(0,line.find(":"));
if (line2.length() < 1){ // problem with last line --> he thinks theres another one :/
break;
}
while (device.find(" ")){ \\ <---- help needed here
size_t pos = device.find(" "); \\ <---- help needed here
device.erase(pos,2); \\ <---- help needed here
}
cout << "device = " << device << "\n";
test << "device = " << device << "\n";
LineData oLineData;
istringstream lineStream(line2); // dump it into a stringstream so we can use istream_iterators on it
vector<int> lineNums;
copy(istream_iterator<int>(lineStream), istream_iterator<int>(), back_inserter(lineNums)); // copy the ints into the vector
oLineData.setName(device);
oLineData.setData(lineNums);
oNetDevList.push_back(oLineData );
}
}
for (int i = 0; i < oNetDevList.size(); i ++){
cout << oNetDevList[i].getName() <<"\n";
for (int y=0; y < oNetDevList[i].getDataLength(); y++){
cout <<"y =" <<y <<" " << oNetDevList[i].getData(y) <<"\n";
}
cout << "----------\n\n";
}
return 0;
}
works like a charm exept one thing !!!! i just cant seem to remove " " in a string. think im doing it all wrong
device names are read in like this
" lo"
" eth0"
" eth1"
i need to remove those white space so i need to get this
"lo"
"eth0"
"eth1"
also have a problem with the last line, prog thinks there is another one after the last one so im using a stupid if to get out of the while lus.
CODE
if (line2.length() < 1){ // problem with last line --> he thinks theres another one :/
break;
}