Reading file and splinting according to format

Dear friends,
Please the bellow code . I am trying to read the file ie /proc/net/wireless and printing this is working but I want only specific things from that file for that I am splinting the file by using strtok but it is not giving any thing. please see the bellow code.

My intention is to read the level field in that file.

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
49
50
51
52
53
54
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main() {
string line;
const char* ifname = "wlan0";
ifstream input ("/proc/net/wireless");

if(!input.is_open())
{
cout << "Couldn't open the file " << endl;
}

    while(getline(input, line)){
        char *bp = strdup(line.c_str());

while(*bp && isspace(*bp))
bp++;
if(strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':')
            {
bp = strchr(bp, ':');
bp++;

              bp = strtok(bp, " ");
             cout << bp ;
           
            bp = strtok(NULL, " ");
              if(strchr(bp,'.') != NULL)
                
		cout << bp ;           
              bp = strtok(NULL, " ");
              if(strchr(bp,'.') != NULL)
		cout << bp;              
              bp = strtok(NULL, " ");
              if(strchr(bp,'.') != NULL)
              
              cout << bp;
              bp = strtok(NULL, " ");
		cout << bp;
              bp = strtok(NULL, " ");
	           cout << bp;
              bp = strtok(NULL, " ");
		cout << bp;

//        std::cout <<"End of line"<<std::endl;
//     delete bp;
    }
    return 0;
}

}

Output :

nothing it is giving. but compiling without errors.

Best Regards
Babu
Last edited on
use code tag, it's ureadable...
Please see now.
Here , in this code just I am trying to print individual token at time and not storing right now
Please tell me any one how to get out of this problem. please.
Consider using C++ for text manipulation, it makes things easier on the eyes:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;

const string IFNAME = "wlan0";

int main()
{
    ifstream input("/proc/net/wireless");
    if(!input)
    {
        cout << "Couldn't open the file\n";
        return 1;
    }

    string line;
    while(getline(input, line)) {
        istringstream bp(line);
        string fname;
        bp >> fname;
        if(fname == IFNAME + ':')
        {
            replace(line.begin(), line.end(), '.', ' ');
            cout << "first word: " << fname << '\n';
            string word;
            while(bp >> word)
                cout << "next word: " << word << '\n';
        }
    }
}
Last edited on
Topic archived. No new replies allowed.