我一直致力于代码:
以下是我到目前为止所写的内容。但是iszeroOrnewfile总是变成0,所以不要进入不同的情况。我正确接近这个吗?
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define buf 128
int main (int argc, char** argv)
{
int fd;
char buff[buf]={0} ;
int iszeroOrnewfile=1;
int iszero=0;
if(!(argv[0]>0))
printf("insert positive integer");
fd = open("./sample.txt",O_RDWR|O_CREAT);
pread ( fd,buff,buf,0);
for (int i=0;i<buf;i++)
{
if((0 != buff[i]) || ('0' != buff[i]) )
{ iszeroOrnewfile = 0;}
if('0' == buff [i]);
{ iszero = 1;}
}
if (iszeroOrnewfile == 1 )
{
if (iszero !=1)
write(fd, "0",strlen("0"));
}
else if(iszeroOrnewfile ==0)
{
truncate ("./sample.txt" , 0);
write(fd, "0",strlen("0"));
}
}
答案 0 :(得分:2)
以下代码可能有用:
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define buf 128
int main (int argc, char** argv)
{
int fd;
char buff[buf]={0} ;
int iszeroOrnewfile=1;
int iszero=0;
int isFileCreated = 0;
int len = 0;
int i;
if(!(argv[0]>0))
printf("insert positive integer");
fd = open("./sample.txt",O_RDWR);
if(fd==-1 && errno==ENOENT)
{
//File dosen't exists
isFileCreated = 1;
fd = open("./sample.txt",O_RDWR|O_CREAT);
write(fd, "0",strlen("0"));
}
else
{
len = pread ( fd,buff,buf,0);
for (i=0;i<len;i++)
{
if((0 == buff[i]) || ('0' == buff[i]) )
{
iszero = 1;
break;
}
}
if(len==-1 || iszero)
{
truncate ("./sample.txt" , 0);
write(fd, "0",strlen("0"));
}
}
return 0;
}