Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,800 people online right now. Registration is fast and FREE... Join Now!




ifstream help

 
Reply to this topicStart new topic

ifstream help

akira300
14 Mar, 2007 - 06:04 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
CODE
face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:     448       6    0    0    0     0          0         0      448       6    0    0    0     0       0          0
  eth0: 2781926   22358    0    0    0     0          0         0   224670    1344    0    0    0     0       0          0
  eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0


how can i read those numbers with an ifstream ???
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Ifstream Help
14 Mar, 2007 - 06:50 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Do you want to read them as numbers, or the line as a whole into a string?
User is online!Profile CardPM
+Quote Post

akira300
RE: Ifstream Help
15 Mar, 2007 - 03:03 AM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
QUOTE(Amadeus @ 14 Mar, 2007 - 07:50 AM) *

Do you want to read them as numbers, or the line as a whole into a string?


as numbers ph34r.gif
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Ifstream Help
15 Mar, 2007 - 03:16 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(akira300 @ 15 Mar, 2007 - 04:03 AM) *

as numbers ph34r.gif


You need to create variables and just let ifstream do all the work... I think this is what you are trying to do:

CODE

int var1, var2;

ifstream f;

// connect f stream to io port
f.open(FileCreate(...) ); // this isn't quite right

// now read it
f >> var1 >> var2; // can be longer....


Correct me if I am wrong, you are trying to connect the stream to the interface ports, right?

This post has been edited by gregoryH: 15 Mar, 2007 - 03:21 AM
User is offlineProfile CardPM
+Quote Post

akira300
RE: Ifstream Help
15 Mar, 2007 - 07:59 AM
Post #5

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
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;
           }

User is offlineProfile CardPM
+Quote Post

akira300
RE: Ifstream Help
15 Mar, 2007 - 08:29 AM
Post #6

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 14


My Contributions
found out how to delete whitespace in a string

CODE
    device = line.substr(0,line.find(":"));
                size_t pos = device.find(' ');
                while (pos != string::npos){
                    if (pos != string::npos){
                        cout << "pos = " << (int) pos << "\n";
                        device.erase((int)pos,1);
                        cout << "device =" << device << "\n";
                        
                    }else{
                        cout << "niet gevonden";
                    }
                    pos = device.find(' ');
                }


is there a better shorter way to do this ?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month