c++ 读写文件,先判断 OK文件是否存在,然后在读,类似于做个互斥

c++写文件

#include<iostream>
#include <unistd.h>
#include <sstream>
#include<fstream>
using namespace std;
#define DATE_ONE "/root/exercise/c++/chapter_one/read/writing.txt"
#define DATE_ONE_OK "/root/exercise/c++/chapter_one/read/writing_ok.txt"
int targetOneInterval = 10;
int a = 0;
int ProbeOnedatehandle()
{
 if(access("root/exercise/c++/chapter_one/read/writing_ok.txt", 0 ) != -1)
{
 ifstream datefile_ok("/root/exercise/c++/chapter_one/read/writing_ok.txt");
 ostringstream buf_ok;
 char ch_ok;
 string str_ok;
 while(buf_ok&&datefile_ok.get(ch_ok))
 {      
        buf_ok.put(ch_ok);
 }
 str_ok = buf_ok.str();
 datefile_ok.close();
 cout<<"str_ok =  "<<str_ok<<endl;
 if(str_ok == "0")
 {
   
   ofstream out_ok(DATE_ONE_OK,ios::out);
  if(out_ok.fail())
  { 
   cout<<"error\n";
  }
  out_ok<<"1";
  out_ok.close();
  
  ofstream out(DATE_ONE,ios::out);
  if(out.fail())
  {
   cout<<"error\n";
  }
  out<<"ProbeOnedatehandle_writing"<<"  "<<a<<"\n";
  out.close();
 }

}
 else
{
 ofstream out_ok(DATE_ONE_OK,ios::out);
 if(out_ok.fail())
 {
   cout<<"error\n";
 }
 out_ok<<"1";
 out_ok.close();

  ofstream out(DATE_ONE,ios::out);
  if(out.fail())
  {
   cout<<"error\n";
  }
  out<<"ProbeOnedatehandle_writing"<<"  "<<a<<"\n";
  out.close();
}
}
int main()
{
 int count = targetOneInterval;
 while(1)
 {
  while(--count > 0)
  {
   sleep(1);
  }
  a++;
  ProbeOnedatehandle();
  count = targetOneInterval;
 }
}

读文件:

#include<iostream>
#include <sstream>
#include <unistd.h>
#include<fstream>	
#include<string>
using namespace std;
int num = 0;
#define DATE_ONE_OK "/root/exercise/c++/chapter_one/read/writing_ok.txt"
int main()
{
string sendMsg;
 while(1)
 {	
   if ((access("/root/exercise/c++/chapter_one/read/writing_ok.txt", 0)) != -1)
   {
	ifstream datefile_ok("/root/exercise/c++/chapter_one/read/writing_ok.txt");
    ostringstream buf_ok;
    char ch_ok;
	string str_ok;
	while(buf_ok&&datefile_ok.get(ch_ok))
    {       
        buf_ok.put(ch_ok);
    }       
    str_ok = buf_ok.str();
    datefile_ok.close();
	cout<<"str_ok =  "<<str_ok<<endl;

 	if(str_ok == "1")
	{
         ifstream datefile("/root/exercise/c++/chapter_one/read/writing.txt");
	 ostringstream buf;	
	 char ch;
	 string str;
	 while(buf&&datefile.get(ch))
	 {	
		buf.put(ch);
	 }	
	 str = buf.str();
	 datefile.close();
      if(str.size() == 0)
	 {
		sleep(1);
	 }
	 else
	 {
		sendMsg = str;
        cout<<"sendMsg = "<<sendMsg;
//		pthread_mutex_lock(&g_listProbeSendMutdata); 
//		g_listProbeMsgdata.push_back(sendMsg);
//		pthread_mutex_unlock(&g_listProbeSendMutdata); 
//		//system("rm -rf /data/local/probe/onedata.txt");
		fstream file("/root/exercise/c++/chapter_one/read/writing.txt", ios::out);
	
	        ofstream out_ok(DATE_ONE_OK,ios::out);
	        if(out_ok.fail()) 
	        {    
	          cout<<"error\n";
	        } 
	        out_ok<<"0"; 
	        out_ok.close();
	 
         } 
   }
   else
   {
      cout<<"00000000"<<endl;
	sleep(1);
   }

} 
}
}	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值