分类: C/C++
2006-01-25 09:30:51
#include
int err=0;
static void readcsv(char *filename,char fengei,int yssl,int hs,char *ren[yssl])
{
int end=1;
FILE *in;
char zifuread;
int hsjs=1;
int ysjs=0;
char zifu[yssl][65535];
int ysslxb=0,zifuxb=0;
if((in=fopen(filename,"r"))!=NULL)
{
while ((zifuread=fgetc(in))!=EOF)
{
if(zifuread==10)
{
hsjs++;
if(end==0)
{
ren[ysslxb]=zifu[ysslxb];
break;
}
continue;
}
if(hsjs==hs)
{
if(zifuread==fengei)
{
ren[ysslxb]=zifu[ysslxb];
ysslxb++;
zifuxb=0;
end=0;
ysjs++;
continue;
}
if(yssl>=ysjs)
{
zifu[ysslxb][zifuxb]=zifuread;
zifuxb++;
continue;
}
}
}
}
else
{
printf(""%s" file not found!!!n",filename);
err=1;
}
}
main()
{
int yssl=2;
char *ren[yssl];
int hs=2;
int i=0;
readcsv("filename",',',yssl,hs,ren);
if(err==0)
{
for(;i
printf("ren[%d]=%sn",i,ren[i]);
}
}
}
调用方式为readcsv(文件名,'分割符号',元素的个数,需要读取第几行的行数,用来存储的字符指正数组)
修改了几个地方
void readcsv(const char *filename,const char fengei,const int yssl,const int hs,char ren[yssl][65535])
{
int end=1;
FILE *in;
char zifuread;
int hsjs=1;
int ysjs=0;
int ysslxb=0,zifuxb=0;
int i=0;
for(;i
memset(ren[i],0,sizeof(ren[i]));
}
if((in=fopen(filename,"r"))!=NULL)
{
while ((zifuread=fgetc(in))!=EOF)
{
if(zifuread==10)
{
hsjs++;
if((end==0)&&(yssl>ysjs))
{
break;
}
continue;
}
if(hsjs==hs)
{
if(zifuread==fengei)
{
ysslxb++;
zifuxb=0;
end=0;
ysjs++;
continue;
}
if(yssl>=ysjs)
{
ren[ysslxb][zifuxb]=zifuread;
zifuxb++;
continue;
}
}
}
fclose(in);
}
else
{
printf(""%s" file not found!!!n",filename);
}
}
另一个函数:
int maxline(char *filename)
{
FILE *in;
char zifu;
int line=0;
if((in=fopen(filename,"r"))!=NULL)
{
while((zifu=fgetc(in))!=EOF)
{
if(zifu==10)
{
line++;
}
}
fclose(in);
return (line);
}
else
{
printf(""%s" file not found!!!n",filename);
line=-1;
return (line);
}
}
生成脚本,修改,编辑,删除文件中的某一部分:
#include
#include
#include
int wfile(char *filename,char moshi,int yssl,char *ysju[yssl],char *sghsj[yssl])
{
FILE *out;
int i=0;
int j=0;
char bring[]="#!/bin/sh";
if ((out=fopen("tmp.h","w"))!=NULL)
{
fprintf(out,"%sn",bring);
if(moshi=='A')
{
j=1;
fprintf(out,"echo "");
for (i=0;i
fprintf(out,"%s%c ",sghsj[i]);
}
fprintf(out,"">>%sn",filename);
}
if(moshi=='D')
{
j=1;
fprintf(out,"cat %s | awk '!/^",filename);
for (i=0;i<(yssl-1);i++)
{
fprintf(out,"%s[[:blank:]]",ysju[i]);
}
fprintf(out,"%s",ysju[i]);
fprintf(out,"+/{print}'>tmp.confn");
fprintf(out,"mv tmp.conf %s -fn",filename);
}
if(moshi=='M')
{
char *old;
char *new;
j=1;
for (i=0;i
if(ysju[i]!=sghsj[i])
{
old=ysju[i];
new=sghsj[i];
}
}
fprintf(out,"sed -e '/^");
for (i=0;i<(yssl-1);i++)
{
fprintf(out,"%s.",ysju[i]);
}
fprintf(out,"%s$/",ysju[i]);
fprintf(out,"{s/");
fprintf(out,"%s",old);
fprintf(out,"/");
fprintf(out,"%s",new);
fprintf(out,"/");
char aa=';';
fprintf(out,"%c",aa);
fprintf(out,"}'");
fprintf(out," %s>tmp.confn",filename);
fprintf(out,"mv tmp.conf %s -fn",filename);
}
fclose(out);
if (j==1)
{
chmod("./tmp.h",S_IRUSR|S_IWUSR|S_IXUSR);
system("./tmp.h");
remove("tmp.h");
return 0;
}
}
else
{
printf("file open bad!!!!n");
return -1;
}
}
main()
{
char *filename="user.conf";
char moshi='M';
int yssl=4;
int test;
char *ysju[yssl];
char *sghsj[yssl];
ysju[0]="tom";
ysju[1]="20";
ysju[2]="female";
ysju[3]="cn";
sghsj[0]="tom";
sghsj[1]="20";
sghsj[2]="male";
sghsj[3]="cn";
test=wfile(filename,moshi,yssl,ysju,sghsj);
}