如何用C语言创建一个HTML文件(或者是一个超链接文件)?
1、C语言不适合用于写网页,虽然可以用C语言写CGI程序,但是这种方法现在已经被PHP/ASP/JSP等动态页面技术取代,所以并不常用。
2、网页代码是基于HTML语言的。进入到WEB2.0以后开始出现大量的动态网页,也就是通过服务器语言如ASP,PHP,CGI等加上各种数据库实现与用户之前的动态交互。总体来说实现网页的显示还是HTML,只是通过这些语言来控制HTML而已。
用C语言删除一个TXT文件中的超链接
本代码测试数据:
超衔接格式我用的普通html的a标签格式,识别前后匹配字符串分别是”a href=”和”/a”。
如果你想要识别其他字符串并删除,请修改函数int deleteHREF(char *str,int len)中的代码:
char strB[]=”a href=”,strE[]=”/a”;
程序功能说明:
识别每行数据中对应的超衔接字符串,并删除。可以一行对应多个超衔接文本。
不识别跨行的超衔接!!!!(一条超衔接,分两行写,这种不识别,注意!!)。
运行后,会在原文件的地址下新建一个new开头的文件存放删除后的数据。
比如源数据c:\list.txt。执行代码后就会新建c:\new_list.txt。这就是你要的数据。
#include stdio.h
#include malloc.h
#include conio.h
#include string.h
#define MAXC 500//预估行最大字符数量,必须大于实际数量,可改大,实际代码会动态分配空间,不会浪费内存
#define MAXFPATH 100//路径最大字符
#define MAXFNAME 50 //文件名最大字符
typedef struct fpInfo
{
char path[MAXFPATH];
char fname[MAXFNAME];
char **content;
int nrow;//行数
int *nCol;//实际每行字数(同时nCol[]+1也是行内存大小)
}FPINFO;
FPINFO *init();
void printfTXT(FPINFO *fpInfo);//打印文本内容
void meError(void *p);//内存申请失败
int deleteHREF(char *str,int len);//删除一行字符串中超衔接文本,删除成功返回1,失败返回0
int delStr(int indexB,int indexE,char *str);//删除str中下标indexB到indexE的字符,返回删除后的字符串长度
void delFromTXT(FPINFO *fpInfo);//从文本中删除超衔接
int writeFile(FPINFO *fpInfo);//一次写入一行
int main()
{
FPINFO *fpInfo=NULL;
fpInfo=init();
if(fpInfo)
printfTXT(fpInfo);
delFromTXT(fpInfo);
printfTXT(fpInfo);
writeFile(fpInfo);
return 0;
}
void delFromTXT(FPINFO *fpInfo)//从文本中删除超衔接
{
int i;
printf(“\n开始从文本中删除所有超衔接内容\n”);
for(i=0;ifpInfo-nrow;i++)
deleteHREF(fpInfo-content[i],fpInfo-nCol[i]);
printf(“删除完成\n\n”);
}
int deleteHREF(char *str,int len)//删除一行字符串中超衔接文本,删除成功返回1,失败返回0
{
int i,m,n,flag,flagB,flagE,lenB,lenE,indexB,indexE;
char strB[]=”a href=”,strE[]=”/a”;
lenB=strlen(strB),lenE=strlen(strE);
while(1)
{
flag=0;
flagB=0;
flagE=0;
for(i=0;ilen;i++)
{
if(flagB)//首字符串匹配成功,开始寻找尾字符串
{
m=i;
for(n=0;nlenE;n++)// 对比超衔接尾字符串
{
if(str[m]!=strE[n])
break;
m++;
}
if(n==lenE)//尾字符串也匹配成功
flagE=1,indexE=m-1;
}
else
{
m=i;
for(n=0;nlenB;n++)// 对比超衔接首字符串
{
if(str[m]!=strB[n])
break;
m++;
}
if(n==lenB)//首字符串匹配成功,下次循环从对应首字符串之后开始
flagB=1,i=m-1,indexB=m-lenB;
}
if(flagB flagE)//首尾都匹配说明找到一个完整的衔接,执行删除衔接,行字符串移位,并开始再次检查该行字符串是否有其他衔接
{
len=delStr(indexB,indexE,str);
flag=1;
break;
}
}
if(!flag)//复查没有找到新的匹配字符串,结束查找
break;
}
return 0;
}
int delStr(int indexB,int indexE,char *str)//删除str中下标indexB到indexE的字符,返回删除后的字符串长度
{
str[indexB]=0;
strcat(str,str[indexE+1]);
return strlen(str);
}
void printfTXT(FPINFO *fpInfo)//打印文本内容
{
int i;
printf(“当前txt文本中的内容是:\n—————-\n”);
for(i=0;ifpInfo-nrow;i++)
printf(“%s\n”,fpInfo-content[i]);
printf(“—————-\n”);
}
FPINFO *init()
{
int row=0,len=0,*ncSave=NULL;
FILE *fp=NULL;
char filePath[MAXFPATH+MAXFNAME]={0};
char str[MAXC]={0},*rCtt=NULL,**cSave=NULL;
FPINFO *fpInfo=(FPINFO *)malloc(sizeof(FPINFO));
meError(fpInfo);
printf(“请输入要读取的文件路径(不包含文件名,小于%d个字符):\n”,MAXFPATH);
scanf(“%s”,fpInfo-path);
printf(“请输入要读取的文件名称(小于%d个字符):\n”,MAXFNAME);
scanf(“%s”,fpInfo-fname);
strcat(filePath,fpInfo-path);
strcat(filePath,fpInfo-fname);
fpInfo-content=NULL;
fpInfo-nCol=NULL;
fp=fopen(filePath,”rt+”);
if(!fp)
{
printf(“文件打开失败!\n”);
return NULL;
}
fseek(fp,0,SEEK_SET);
while(fgets(str,MAXC,fp))
{
len=strlen(str);
row++;
if(!fpInfo-content)
fpInfo-content=(char **)malloc(sizeof(char *)*row);
else
{
cSave=realloc(fpInfo-content,sizeof(char *)*row);
if(cSave)
fpInfo-content=cSave;
}
fpInfo-nrow=row;
if(!fpInfo-nCol)
fpInfo-nCol=(int *)malloc(sizeof(int)*row);
else
{
ncSave=realloc(fpInfo-nCol,sizeof(int)*row);
fpInfo-nCol=ncSave;
}
fpInfo-nCol[row-1]=len;
rCtt=(char *)malloc(sizeof(char)*len+1);
meError(fpInfo-content);
meError(rCtt);
memset(rCtt,0,len+1);
strcpy(rCtt,str);
fpInfo-content[row-1]=rCtt;
memset(str,0,MAXC);
}
fclose(fp);
return fpInfo;
}
int writeFile(FPINFO *fpInfo)
{
char filePath[MAXFPATH+MAXFNAME+1]={0};
int i,row=fpInfo-nrow;
FILE *fp=NULL;
strcat(filePath,fpInfo-path);
strcat(filePath,”new_”);
strcat(filePath,fpInfo-fname);
fp=fopen(filePath,”w+”);
if(!fp)
{
printf(“文件创建失败!\n”);
return 0;
}
fseek(fp,0,SEEK_SET);
printf(“\n—-开始写入新的文件—-\n”);
for(i=0;irow;i++)
fprintf(fp,”%s”,fpInfo-content[i]);
printf(“—-新数据保存完成—-\n”);
fclose(fp);
return 1;
}
void meError(void *p)//内存申请失败
{
if(p==NULL)
{
printf(“\n异常:内存申请失败!回车结束程序!\n”);
while(getch()!=’\r’);
exit(0);
}
}
C语言提取源文件?
这段代码能把键盘输入的特定字符串后面双引号内的文字提取出来存入XYZ.txt文件中。在提取过程中滤除了源文件中的’\n’和’ ‘。供参考……
#include “stdio.h”//
#include “string.h”//
#include “stdlib.h”//
void main(void){
FILE *pR,*pW;
char CoStr[20],ln,ch,i=0;
if(!(pR=fopen(“ABC.txt”,”r”))){
printf(“源文件打开失败…”);
exit(0);
}
printf(“请输入特定字符串…\nCoStr=”);
ln=strlen(gets(CoStr));
do{
fseek(pR,-i,SEEK_CUR);
for(i=0;iln;i++)
if((ch=getc(pR))!=CoStr[i]) break;
if(ch==EOF){
printf(“没有发现特定字符串%s!\n”,CoStr);
exit(0);
}
}while(iln);
if(!(pW=fopen(“XYZ.txt”,”w”))){
printf(“创建目标文件失败…”);
exit(0);
}
while((ch=getc(pR))!='”‘ ch!=(char)176);
while((ch=getc(pR))!='”‘ ch!=(char)177){
if(ch==’\n’ || ch==’ ‘) continue;
putc(ch,pW);
}
fclose(pW);
fclose(pR);
printf(“文件已成功建立,名为XYZ.txt\n”);
}
c语言提取html标签内容
#include stdio.h
#include math.h
void fetch_str(char *str_in, char *str_out);
int main(){
char test[] = “aThis is the … string/a”;
char result[256];
fetch_str(test, result);
printf(“\ntest\t=%s\n”, test);
printf(“\nresult\t=%s\n”,result);
return 1;
}
void fetch_str(char *str_in, char *str_out)
{
char begin_str[] = “a”;
char end_str[] = “/a”;
int index_end =0;
int index_begin=0;
int flag_begin =0;
int flag_end =0;
int str_index=0;
int i;
// to find the max index of str_in
while(str_in[str_index]!=’\0′)
{
str_index++;
}
str_index–;
//printf(“%s %s”, begin_str, end_str);
int count=0;
while(str_in[count]!=’\0′)
{
// to find the begin index of the target string
if( flag_begin==0 count=(str_index-2) )
{
if( str_in[count]==begin_str[0] str_in[count+1]==begin_str[1] str_in[count+2]==begin_str[2] )
{
flag_begin=1;
index_begin=count+3;
}
}
// to find the end index of the target string
//if( flag_end==0 count=(str_index-3) )
if(count=(str_index-3) )
{
if( str_in[count]==end_str[0] str_in[count+1]==end_str[1] str_in[count+2]==end_str[2] str_in[count+3]==end_str[3])
{
flag_end=1;
index_end=count-1;
}
}
count++;
}
//printf(“\nbegin_index=%d, end_index=%d\n”, index_begin, index_end);
// to copy the target string to str_out
count=0;
for(i=index_begin;i=index_end;i++)
{
str_out[count]=str_in[i];
count++;
}
str_out[count]=’\0′;
return;
}
c语言提取html中的信息
1. 如果你懂编程序:
使用字符串匹配算法,例如KMP算法,AC算法等。
AC算法比较好,网络上可能有现成的,弄一个源代码,再修改下就可以。
2. 如果你不懂编程:
用word,ultraedit等编辑工具,直接搜索你要的字符串就可以了吧。