学习SHELL过程中自己写的简单例子
时间:2006-02-27 来源:77902543
http://cyberfan.5151j.net/article.asp?id=1575
-----------------------------------------------------------
./a.out 2 A //在串的第2个位置插入A,结果为aAsdf
#include "unistd.h"
#include "stdlib.h"
main(int argc,char *argv[])
{
char a[]="asdf";
int b=strlen(a);
int i=atoi(argv[1]);
char *s=argv[2];
for(;(i-1) < b;b--)
a[b]=a[b-1];
a[b]=*s;
printf("%s\n",a);
return 0;
}
----------------------------------------------------------
#include "stdlib.h" printf("%d\n",atoi(argv[2])); //将字符类型的数据转换为int类型的
#include "sys/types.h"#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "unistd.h"
main(int argc,char* argv[])
{
int a;
char st1[]="asdasdasd";
struct stat st2 ;
a=open(argv[1],O_RDWR|O_APPEND);
//chown(argv[1],atoi(argv[2]),atoi(argv[3]));
//write(a,st1,strlen(st1));
//chmod(argv[1],0700);
//truncate(argv[1],100);
//link(argv[1],argv[2]);
//symlink(argv[1],argv[2]);
//unlink(argv[2]);
//rename(argv[1],argv[2]);
//mkdir(argv[2],0753);
//rmdir(argv[2]);
//chdir("/root");
//printf("%s\n",getcwd(NULL,0));
stat(argv[1],&st2);
printf("%s size is %d\n",argv[1],st2.st_size);
if(access(argv[1],R_OK|W_OK|X_OK)==0)
printf("You have all Permission\n");
else
printf("Non't have all Permission\n");
close(a);
} main(int argc,char* argv[])
{
//FILE *a;
//char sa[]="asdasdasd";
//a=fopen(argv[1],"r+");
//fputs(sa,a);
//fclose(a); /*FILE *a;
char s;
a=fopen(argv[1],"r+");
while((s=fgetc(a))!=EOF)
printf("%c",s);
printf("\n");
fclose(a);*/ FILE *a;
char s[20];
a=fopen(argv[1],"r+");
while((fgets(s,20,a))!=NULL)
printf("%s",s);
printf("\n");
fclose(a); } ---------------------------------------------------------------------
相关阅读 更多 +