0

I am trying to read data from a .pcap file which is simultaneously being written to by USBPcap. I am doing it as mentioned in this answer, but my ifs.is_open() call always fails.

Here is my code :

int main()
{
    std::string fullFilename = "D:\\Ročníkový projekt\\USBCap\\sample2.pcap";
    std::ifstream ifs(fullFilename.c_str(), std::fstream::binary | std::fstream::in);
    long int i = 0;
    if (ifs.is_open())
    {
        char c;
        while (true)
        {
            while (ifs.get(c)) std::cout << c << "\n";
            if (!ifs.eof()) break;
            ifs.clear();
        
            Sleep(500);
        }
    }
}

Any idea what is wrong?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Peter
  • 57
  • 1
  • 7
  • Visual studio's implementation of `ifstream` has an extra parameter to allow you to specify file sharing: https://learn.microsoft.com/en-us/cpp/standard-library/basic-ifstream-class?view=vs-2019#basic_ifstream – Alan Birtles Jul 31 '20 at 20:09
  • @AlanBirtles thank you for your answer, unfortunately, changing it to `std::ifstream ifs(fullFilename.c_str(), std::fstream::binary | std::fstream::in, _SH_DENYNO);` didnt help me and i still cant open it. – Peter Jul 31 '20 at 20:28
  • 1
    Usbpcap has probably locked the file then, not much you can do – Alan Birtles Jul 31 '20 at 20:55

0 Answers0