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);
}
}
}
}