Deleting file contents synchronization

Ok this is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
	}
}


1
2
3
4
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
Fedora 20
Netcat just receives text files on this side.

Thank you all for your time.
if, as you stated, you are filling log, through your own thread,
simply write your log to std::sting, and after you write check the size of this string - if it hit your sending threshold -> write it to file and then send it.

some pseudo code:

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
55
CriticalSection cs; //dont forget to init and deinit it before using

std::vector<std::string> buffer; //shared data, so use critical sections when accessing it

event read_event=0; //create auto clear event

void writingThread()
{
	std::string writeBuffer;
	
	while ( some_closing_event ) //set this event when closing this app
	{
		writeBuffer += "some log";
		
		//if write buffer is bigger than 1024 * 1000 bytes its time to send
		if ( writeBuffer.size() * sizeof( TCHAR) > 1024*1000)
		{
			EnterCriticalSection(&cs);
			buffer.push_back(writeBuffer);
			LeaveCriticalSection(&cs);
			writeBuffer.clear(); //shared data
			SetEvent(read_event);
		}
	}
}


void sendingThread()
{
PROCESS_INFORMATION processInfo = {0};
std::string sendBuffer;

//fill processInfo

 while( some_closing_event)
 {
	waitforsingleobject(read_event); //if read_event is auto_clear it will reset after resuming sending thread
	
	EnterCriticalSection(&cs);
	sendBuffer = buffer.pop(). //shared data
	writeBuffer.clear();
	
	-- >write to file sendBuffer string;
// you dont even need to create file, because with create process you can send sendBuffer content straight through process pipeline
	
	sendBuffer.clear();
	
	//instead of "system" use create proces through CreateProcess, because then you can WaitForSingleObject until it is done
	CreateProcess( [...] , &processInfo);
	
	WaitForSingleObject( processInfo.hProcess, INFINITE); //you can add some timout if needed

delete file; //if used
 }
}


Thx for the help I went for mutex and just locked the thread access but it didn t work quite as planned so I just set a time for a delete function, makes a few duplicates when sending but it is not too bad gets the job done.
Topic archived. No new replies allowed.