pthread write in file

Well I have program which have 5 thread one of them is write
I know that all the other thread work correctly but in write thread
it doesn't write to file correctly i try to write in specific line but sometimes it dosen't write in that line and sometimes it wont write at all to be clear I have buffer which the content is like "12-3" 3 is the line and 12 is the result i want to write in line

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

void *writeToFile(void* param)
{
	//!is_killed && !add_is_killed && !sub_is_killed && !mul_is_killed && !div_is_killed
	while(true){
		for(int i=0 ; i<5 ; i++){
			if(final_buff[i]!=""){
				vector<string> v;
				sem_wait(&final_buff_full);
	pthread_mutex_lock(&mutex1);
	string numbers = final_buff[i];
	final_buff[i]="";
	v= getOperands(numbers , '-');
	int lineNo;
	istringstream ss0(v[1]);
	ss0 >> lineNo;
	string toWrite = v[0];
	cout << "is writing : " << numbers << endl;	
	pthread_mutex_unlock(&mutex1);
	sem_post(&final_buff_empty);

	
	
    fstream file( "output.txt" ) ;

     unsigned currentLine = 1 ;
     while ( currentLine < lineNo )
     {
          // We don't actually care about the lines we're reading,
          // so just discard them.
          file.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;
          ++currentLine ;
     }

     // Position the put pointer -- switching from reading to writing.
     file.seekp(file.tellg()) ;
     
     if(file << toWrite) 
     	{
     		cout << "written : " << toWrite << "-" << lineNo <<endl ;
     	}
     //pthread_mutex_unlock(&mutex2);
	//------------
			}
			
		}
	}
Topic archived. No new replies allowed.