Originally posted by: SystemAdmin
--------------------example1 begin---------------------------------
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <pthread.h>
#include <stdio.h>
char m_controlFile
200 + 1 = "./log/control.ini";
std::fstream m_controlStream;
int readfile(int pid);
int main(int argc, char* argv[])
{
readfile(2);
}
int readfile(int pid)
{
int ret = 5;
m_controlStream.open(m_controlFile, std::ios:out);
printf("pid %d: opened up\n", pid, ret);
if (m_controlStream.fail())
{
printf("pid %d: open fail.\n", pid);
return -1;
}
else if (m_controlStream.bad())
{
printf("pid %d: open bad.\n", pid);
return -1;
}
else if (m_controlStream.eof())
{
printf("pid %d: open eof.\n", pid);
return -1;
}
else
{
m_controlStream << ret;
}
printf("pid %d: output is %d\n", pid, ret);
m_controlStream.close();
printf("pid %d: close\n", pid, ret);
for (int i = 0; i < 2; i++)
{
m_controlStream.open(m_controlFile);
printf("pid %d: opened up\n", pid, ret);
if (m_controlStream.fail())
{
printf("pid %d: open fail.\n", pid);
return -1;
}
else if (m_controlStream.bad())
{
printf("pid %d: open bad.\n", pid);
return -1;
}
else if (m_controlStream.eof())
{
printf("pid %d: open eof.\n", pid);
return -1;
}
else
{
m_controlStream >> ret;
}
printf("pid %d: output is %d\n", pid, ret);
m_controlStream.close();
printf("pid %d: close\n", pid, ret);
}
}
------------------------------example1 end--------------------------
------------------------------example2 begin------------------------
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <pthread.h>
#include <stdio.h>
char m_controlFile
200 + 1 = "./log/control.ini";
std::fstream m_controlStream;
int readfile(int pid);
int main(int argc, char* argv[])
{
readfile(2);
}
int readfile(int pid)
{
int ret = 5;
m_controlStream.open(m_controlFile);
printf("pid %d: opened up\n", pid, ret);
if (m_controlStream.fail())
{
printf("pid %d: open fail.\n", pid);
return -1;
}
else if (m_controlStream.bad())
{
printf("pid %d: open bad.\n", pid);
return -1;
}
else if (m_controlStream.eof())
{
printf("pid %d: open eof.\n", pid);
return -1;
}
else
{
m_controlStream << ret;
}
printf("pid %d: output is %d\n", pid, ret);
m_controlStream.close();
printf("pid %d: close\n", pid, ret);
for (int i = 0; i < 2; i++)
{
m_controlStream.open(m_controlFile);
printf("pid %d: opened up\n", pid, ret);
if (m_controlStream.fail())
{
printf("pid %d: open fail.\n", pid);
return -1;
}
else if (m_controlStream.bad())
{
printf("pid %d: open bad.\n", pid);
return -1;
}
else if (m_controlStream.eof())
{
printf("pid %d: open eof.\n", pid);
return -1;
}
else
{
m_controlStream >> ret;
}
printf("pid %d: output is %d\n", pid, ret);
m_controlStream.close();
printf("pid %d: close\n", pid, ret);
}
}
-------------------------------example2 end--------------------------
under AIX5.1 run example2,output is:
pid 2: opened up
pid 2: output is 5
pid 2: close
pid 2: opened up
pid 2: output is 5
pid 2: close
pid 2: opened up
pid 2: output is 5
pid 2: close
under AIX5.1 run example2,output is:
pid 2: opened up
pid 2: output is 5
pid 2: close
pid 2: opened up
pid 2: output is 5
pid 2: close
pid 2: opened up
pid 2: open eof.
check the source of example1 and example2, we found the difference is:
Example1:m_controlStream.open(m_controlFile, ios:ut);
Example2:m_controlStream.open(m_controlFile);
who can tell me why?
#AIX-Forum