#include
#include
#include
#include
#include
#define PERM 0755
#define Buff 512
/*string open()
{
static char filename[]="t1.txt";
int handle;
handle=open(filename,O_RDONLY);
if (handle==-1)
printf ("%s does not exist\n",filename);
else
printf ("open success\n");
}*/
string close()
{
static char filename[]="t1.txt";
int handle;
int ret;
handle=open(filename,O_RDONLY);
if (handle==-1)
printf ("%s does not exist\n",filename);
else
printf ("open success\n");
ret=close(handle);
if (ret==0)
printf ("close file success\n");
else
printf ("close file error \n");
}
string creat()
{
static char filename[]="t1.txt";
int handle;
handle=creat(filename,PERM);
if (handle==-1)
printf ("%s create fail\n",filename);
else
printf ("open success\n");
}
string write(int argc,char *argv)
{
char buffer[Buff];
int source,dest;
int count;
if(argc!=3)
{ printf ("sorry input error\n");
exit(1);}
source=open(argv[1],O_RDONLY);
if (source==-1)
{ printf ("sorry input error\n");
exit(1);}
dest=creat(argv[2],PERM);
if (dest==-1)
{ printf ("%s creat error \n",argv[2]);
exit(1);}
while((count=read(source,buffer,Buff))>0)
write(dest,buffer,count);
}
string read()
{
static char filename[]="t1.txt";
char buffer[Buff];
int handle,I;
int total=0;
handle=open(filename,O_RDONLY);
if (handle==-1)
{printf ("open file fail \n");
exit(1);}
else
while ((I=read(handle,buffer,Buff))>0)
total+=I;
printf ("The total character in %s is %d\n",filename,total);
}
string chmod()
{
static char file[]="t1.txt";
if (chmod(file,0700)==-1)
{
printf (stderr,"chmod for file is error\n",file);
exit(1);
}
printf ("%s mod for file has been changed\n",file);
}
main()
{
printf("0 exit\n");
printf("1 creat a new file\n");
printf("2 write\n");
printf("3 read a file\n");
printf("4 change the potence\n");
printf("5 check the and exit\n");
printf("please input a number from 0 to 5:\n");
int x;
scanf("%d\n",&x);
switch(x)
{
case '0':close();
case '1':creat();
case '2':write(int argc,char *argv);
case '3':read();
case '4':chmod();
}
}
运行结果:
6.c:20: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘close’
6.c:38: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘creat’
6.c:49: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘write’
6.c:69: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘read’
6.c:85: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘chmod’
6.c: 在函数 ‘main’ 中:
6.c:110: 错误:提供给函数 ‘close’ 的实参太少
6.c:111: 错误:提供给函数 ‘creat’ 的实参太少
6.c:112: 错误:expected expression before ‘int’
6.c:112: 错误:提供给函数 ‘write’ 的实参太少
6.c:113: 错误:提供给函数 ‘read’ 的实参太少
6.c:114: 错误:提供给函数 ‘chmod’ 的实参太少
本菜鸟实在是不明白到底源代码哪里出问题了,希望大虾们能够帮忙解决一下~~~