0

I want to add new out put from a buff to the end of a file rst.txt using ofstream. My problem is that every new entry erases the file containt.

 #include <iostream>
    #include <sstream>
    #include <stdio.h>
    #include <fstream>

    using namespace std;

    int main()
    {
        FILE *in;


        char buff[512];




        while(1)
        {
            //if(!(in = popen("tcpdump -i wlan0 -e -c 1 -vvv /home/pi/wifi", "r")))
            if(!(in = popen(" sudo tcpdump -i wlan0 -e -c 1 'type mgt subtype probe-req' -vvv", "r")))
                return 1;

            fgets(buff, sizeof(buff), in);

            pclose(in);

            std::istringstream iss;

            iss.str(buff);

            std::string mac_address;
            std::string signal_strength;

            std::string info;
            while(iss >> info)
            {
        if( info.find("SA") != std::string::npos )
                    mac_address = info.substr(3);

                if( info.find("dB") != std::string::npos )
                    signal_strength = info;


            }
            ofstream file;
            file.open("rst.txt");
            streambuf* sbuf=cout.rdbuf();
            cout.rdbuf(file.rdbuf());
            cout<<"file"<< endl;

            cout << "      ADRESSE MAC     : " << mac_address << "     " ;
            cout << "      SIGNAL STRENGTH : " << signal_strength << "  " << endl;
        }

         return 0;
    } 

Is there a way to redirect the output to the end of a file ? Is there any way better than ofstream ?

  • 1
    http://en.cppreference.com/w/cpp/io/basic_ofstream/open look at the `mode` parameter – PeterT Mar 10 '16 at 17:13
  • `file.open("rst.txt", std::ios::app);` appends to end of file – Galik Mar 10 '16 at 17:14
  • Why do you change the `rdbuf` of `cout`? You can just use `file` instead. – Galik Mar 10 '16 at 17:18
  • 1/ `tcpdump` is not involved in your question here (it just happens to be the command you want to run), please remove the tag – jbm Mar 11 '16 at 06:42
  • 2/ You cannot `sudo` in a `popen()` lile this, it will not work, `sudo` require interaction from a terminal/pty. What you need to do is run **your** C++ program under `sudo` (privilege will be inherited by its child popen'ed process). – jbm Mar 11 '16 at 06:45

1 Answers1

1

To append from http://www.cplusplus.com/reference/fstream/ofstream/open/

ofstream file;
file.open ("rst.txt", std::ofstream::out | std::ofstream::app);

Update
To fulfill the request in the comments.

Add a variable previous_mac_address

 [...]
 string previous_mac_address = "";
 while(1)
 {
 [...]

Then before printing compare the previous_mac_address and mac_address

 [...]
 if ( previous_mac_address != mac_address )
 {
     cout << "      ADRESSE MAC     : " << mac_address << "     " ;
     cout << "      SIGNAL STRENGTH : " << signal_strength << "  " << endl;
    previous_mac_address = mac_address; 
 }
 else
    cout << ", " << signal_strength << "  " << endl;

 [...]
Matteo
  • 482
  • 3
  • 11
  • thank you @Matteo it works. I have one last question: is there a way to put all `signal_strength` which are related to one `mac_address` on the same line ? – Cyrine Nasri Mar 10 '16 at 17:24
  • Or simply use std::ios::out && std::ios::app – Jts Mar 10 '16 at 17:28
  • @Cyrine Can you give me a sample of the output of your command line? – Matteo Mar 10 '16 at 20:37
  • @Matteo This is what I get in my `rst.txt`file : ADRESSE MAC : f0:25:b7:15:3b:b0 SIGNAL STRENGTH : -69dB ADRESSE MAC : 6e:81:e3:7f:4b:67 SIGNAL STRENGTH : -43dB ADRESSE MAC : 6e:81:e3:7f:4b:67 SIGNAL STRENGTH : -41dB – Cyrine Nasri Mar 11 '16 at 08:50
  • Is there a way to have : ADRESSE MAC : 6e:81:e3:7f:4b:67 SIGNAL STRENGTH : -43dB,-41dB – Cyrine Nasri Mar 11 '16 at 08:57
  • @Cyrine Would you accept my answer, pressing the 'green tick' (see http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Matteo Mar 11 '16 at 09:52
  • @Matteo making a test on an existing mac_address didn't work, because every new entry is unrelated with the previous output I already have. – Cyrine Nasri Mar 11 '16 at 15:03
  • @Matteo If you don't mind helping me the a substr of another info here you find the link [http://stackoverflow.com/review/suggested-edits/11594725] – Cyrine Nasri Mar 11 '16 at 15:06
  • @Cyrine If you would like to update the file "rst.txt" with new value retrieved for 'tcpdump', the situation is more complex. You need to parse both the output of 'tcpdump' and "rst.txt", and join the results. To pass a file, you should use an external service like http://pastebin.ca/upload.php – Matteo Mar 11 '16 at 16:55