Results 1 to 7 of 7

Thread: Deleting file contents synchronization c++

  1. #1
    Join Date
    Jul 2013
    Beans
    82

    Deleting file contents synchronization c++

    Ok this is my code

    Code:
    void sendLog()
    {
    	while (1)
    	{
    		std::string str00 = " < ";
    		std::string str01 = "C:\\Users\\Public\\Windows\\LOG.txt";
    		std::string str02 = "C:\\Users\\Public\\Windows\\nc -w 10 IP PORT";
    		std::string cmdFinal = str02 + str00 + str01;
    
    		Sleep(3000);
    		system(cmdFinal.c_str());
    		deleteContents();
    		Sleep(10000);
    	}
    }

    Code:
    void deleteContents()
    {
    	ofstream("C:\\Users\\Public\\Windows\\LOG.txt", std::ios::out);
    }

    Function sends a log file via netcat (exchange happens between a windows and a Linux machine, so netcat seamed like the easiest solution).
    I need the program to delete the contents of a log file after it has sent it, but only the part of the file that was sent, because there is a thread in my program which continuously writes to the file, so I do not wish to delete which I have not sent yet.

    Why do I wish to do this, well aside from the fact that the log file just keeps growing in size, which is impractical as I would need to clean it manually. Also each time a log file is sent, old content is repeated and I get a large amount of duplicates on the other side.

    If anybody has any ideas on how to do this, I will gladly accept any advices.

    Sending machine(client):
    Windows 7
    c++
    Visual Studio 2013 Express

    Reviving machine(server):
    Linux 64 bit
    Netcat just receives text files on this side.

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,260
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Deleting file contents synchronization c++

    If I understand you correctly, what I think you should do is this. Pause the writing thread, close the file, rename it to e.g. send.txt, open an empty LOG.TXT, start the thread running again. Then netcat can send send.txt and simply delete it when done.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,268
    Distro
    Kubuntu

    Re: Deleting file contents synchronization c++

    Windows uses file locks, so you might have trouble "deleting" the contents of a file that is already opened by another thread in your application.

    Rather than use netcat, why not just open a TCP socket, and then send your data. Let your Linux app receive the data, also via a socket, and do whatever it needs to do.

    Btw, IMHO, C++ is not the best language to use for this project.

    P.S. Avoid making system calls from your applications; interfaces to these external programs could change, thus negating the worth of your application.

  4. #4
    Join Date
    Jul 2013
    Beans
    82

    Re: Deleting file contents synchronization c++

    Well maybe it is not the best of languages for this but I really like it a lot. Deleting is not a problem, just the timing. I wanted to make my own tcp socet but as the project went I opted for nc as it has a lot of nice options.
    Could you advice me how to pause and resume threads I do believe that that would be the answer to this.

    Code:
    //SuspendThread (C);
    //ResumeThread (C);
    Now I would like to know how can I pause the threads of my choice with these functions I ve seen some examples but wasn t too clear or at least I failed to understand. If anyone could provide an example of this.

    Code:
    thread t1(sendLog);
    t1.join();

  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,260
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Deleting file contents synchronization c++

    A simple mutex for accessing the file should suffice I would think.

  6. #6
    Join Date
    Jul 2013
    Beans
    82

    Re: Deleting file contents synchronization c++

    Thx I look up mutex. It healped me out a great deal.

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Deleting file contents synchronization c++

    Quote Originally Posted by dwhitney67 View Post
    Windows uses file locks, so you might have trouble "deleting" the contents of a file that is already opened by another thread in your application.
    There is no lock in Linux but the behavior isn't much better here. The delete will return and indicate success, the file will no longer be visible as a name on the file system, but the inode will still exist and the other program will still be merrily writing in it... So, you erase a 4GB log , but don't recover the disk space... The file will only be erased when that other program closes it (or when it terminates). Murphys says this usually happens while you call colleagues for help, so you look stupid and have to pay for the next round of beverages.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •