求一行行读文件,比较部分字符串的源代码.高分,顶都有~~~~~~~~~~~~~~

eagleMX 2003-08-21 04:31:23
如题
分不够,再放
...全文
31 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rockrabbit 2003-11-20
  • 打赏
  • 举报
回复
学习。
wflyfox 2003-08-21
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <dos.h>
void readline(FILE *,char *);
void find(char *,char *,char *,char *,char *,char *);
void write(FILE *,char *);
void output(char *,char*,char *,char *,char *);
char login[30],passwd[30],sex[30],phone[50],uname[100];
char *empty="\"\"";
char *sim=",";
char *userflag="\"1\"";
int quat=0;
char *birth="0";
char outs[200];
void convert(char *name,FILE *out)
{
//int numcnt=0;
//int filecnt;
FILE *in;//*out;
//char tname[20];
char line[1024];
//filecnt=1;
//sprintf(tname,"%s%s-%d.csv",str1,str,filecnt);
in=fopen(name,"r");
if(in==NULL)
{
printf("can't open source file\n");
exit(1);
}
/*out=fopen(tname,"w");
if(out==NULL)
{
printf("can't open targetfile\n");
exit(1);
}*/


while(!feof(in))
{
memset(line,'\0',30);
readline(in,line);
memset(login,'\0',30);
memset(passwd,'\0',30);
memset(sex,'\0',30);
memset(uname,'\0',100);
memset(phone,'\0',50);

find(line,login,passwd,sex,phone,uname);
if(feof(in)) break;
if(login[0]=='\0')
continue;
else
{
output(login,passwd,sex,phone,uname);
write(out,outs);
/*numcnt++;
if(numcnt>=100)
{
numcnt=0;
fclose(out);
filecnt++;
sprintf(tname,"%s%s-%d.csv",str1,str,filecnt);
out=fopen(tname,"w");
if(out==NULL)
{
printf("can't open targetfile%s\n",tname);
exit(0);
}
}
else*/
fputc('\n',out);
}
memset(outs,'\0',200);
}
fclose(in);
//fclose(out);
}

void readline(FILE *fp,char *ary)
{
char ch;
while((ch=fgetc(fp))!='\n' && ch!=EOF)
*ary++=ch;
}

void find(char* ary1,char *ary2,char *ary3,char *ary4,char *ary5,char *ary6)
{
char *str,ch;
int i=0;

str=strstr(ary1,"acct-login=");
if(!str)
memset(login,'\0',30);
else
{
for(ch=*(str+11);ch!=' ';++i)
{
*(ary2++)=ch;
ch=*(str+12+i);
}
*(ary2)='\0';
}
i=0;
str=strstr(ary1,"acct-passw=");
if(!str)
memset(passwd,'\0',30);
else
{
for(ch=*(str+11);ch!=' ';++i)
{
*(ary3++)=ch;
ch=*(str+12+i);
}
*(ary3)='\0';
}
i=0;
str=strstr(ary1,"sex=男");
if(!str)
strcpy(ary4,"\"0\"");
else
{
str=strstr(ary1,"sex=");
if(!str)
strcpy(ary4,"\"0\"");
else
strcpy(ary4,"\"1\"");
}

i=0;
quat=0;
str=strstr(ary1,"fee-phone=");
if(!str)
memset(phone,'\0',30);
else
{
for(ch=*(str+10);!(ch=='"'&&quat);++i)
{
quat=1;
*(ary5++)=ch;
ch=*(str+11+i);
}
*(ary5++)='"';
*(ary5)='\0';
}

i=0;
str=strstr(ary1,"user-name=");
if(!str)
memset(uname,'\0',100);
else
{
for(ch=*(str+10);ch!='/';++i)
{
*(ary6++)=ch;
ch=*(str+11+i);
}
*(ary6)='\0';
}
return;

}

void write(FILE *fp,char *ary)
{int count,i;
i=strlen(ary);
for(count=0;count<i;count++)
fputc(ary[count],fp);
}

//ary1 user-account,ary2 user-passd,ary3 sex,ayr4 contract-phone,ary5 user-name
void output(char *aryy1,char *aryy2,char *aryy3,char *aryy4,char* aryy5)
{
int i;
strcat(outs,aryy1);//用户名
strcat(outs,sim);//分隔符
strcat(outs,aryy2);//密码
strcat(outs,sim);//分隔符
strcat(outs,aryy1);//空 user_question
strcat(outs,sim);//分隔符
strcat(outs,aryy2);//空 user_answer
strcat(outs,sim);//分隔符
strcat(outs,empty);//空 user_email
strcat(outs,sim);//分隔符
strcat(outs,userflag);// user_flag
strcat(outs,sim);//分隔符
strcat(outs,aryy5);// real_name
strcat(outs,sim);
strcat(outs,aryy3);//性别 user_sex
strcat(outs,sim);//分隔符
strcat(outs,"\"0000-00-00\"");//user_birth
strcat(outs,sim);
strcat(outs,empty);//user-num
strcat(outs,sim);//empty
if(strlen(aryy4)>0)
strcat(outs,aryy4);
else
strcat(outs,empty);
strcat(outs,sim);
for(i=1;i<=3;i++)
{
strcat(outs,empty);
strcat(outs,sim);
}//postcode,addr,created
strcat(outs,userflag);//user-level
strcat(outs,sim);//sim
for(i=1;i<=2;i++)
{
strcat(outs,empty);
strcat(outs,sim);
}//last-seen,pay
strcat(outs,userflag);//user_img
strcat(outs,sim);
for(i=1;i<=5;i++)
{
strcat(outs,empty);
strcat(outs,sim);
}//user_grade,group,id,ip,mobile
strcat(outs,aryy1);//denglu
i=strlen(outs);
outs[i+1]='\0';

}
我写的工具函数,时间仓促,没有注释
devil97518 2003-08-21
  • 打赏
  • 举报
回复
agree before
bzpscu 2003-08-21
  • 打赏
  • 举报
回复
FILE *fp;
char temp[256];

if((fp=fopen(路径,"a+"))==NULL)
return;

while (!feof(fp))
{
memset(&temp,NULL,sizeof(temp));
if (!fgets(temp,sizeof(temp),fp))
break;
}
fclose(fp);
vcforever 2003-08-21
  • 打赏
  • 举报
回复
用CStdioFile的ReadString试一试吧,
读一行就处理一下!
思危 2003-08-21
  • 打赏
  • 举报
回复
MFC 版
CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName();
CStdioFile file(strFile, CFile::modeRead);
CString strTxt;
CString strKey = "wo";
int n = 0;
while (file.ReadString(strTxt))
{
for(int k=0; (k=strTxt.Find(strKey, k))!=-1; k++)
{
n++;
}
}
TRACE("***** Wo Count : %d", n);
}

测试文件:
wo, wo, wo, and to bo ok wwe wo
mm oo m wo wowo and wowowk wookwokwo.
wwo kkwo kkwkwkowo

搜索关键字: wo

输出:15

思危 2003-08-21
  • 打赏
  • 举报
回复
统计一个文本文件里一个特定字符串出现了多少次:

STL版:
程序:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const std::string Key = "wo";

int main(int argc, char* argv[])
{
if (argc >= 2)
{
std::ifstream ifile(argv[1]);
if (ifile.is_open())
{
int n = 0;
std::string str;
while(ifile >> str)
{
for(int k=0; (k=str.find(Key,k))!=std::string::npos; k++)
{
n++;
}
}
std::cout << "关键字有 : " << n << std::endl;
}
}
else
{
std::cout << "No file for open !" << endl;
}
return 0;
}


测试文件:
wo, wo, wo, and to bo ok wwe wo
mm oo m wo wowo and wowowk wookwokwo.
wwo kkwo kkwkwkowo

搜索关键字: wo

输出:15
flinming 2003-08-21
  • 打赏
  • 举报
回复
FILE *fp;
char temp[256];

if((fp=fopen(路径,"a+"))==NULL)
return;

while (!feof(fp))
{
memset(&temp,NULL,sizeof(temp));
fgets(temp,sizeof(temp),fp);
}
fclose(fp);
FBugFramework 2003-08-21
  • 打赏
  • 举报
回复
0
aimheliopause 2003-08-21
  • 打赏
  • 举报
回复
CStdioFile file("/*somefile*/");
CString strBuf;
while (file.ReadString(strBuf)) {
//do some compare
if (strBuf == _T("/*something*/"))
}
balas 2003-08-21
  • 打赏
  • 举报
回复
上面的函数如果读到'\n' 后那么就跳出了,只能读一行。
用CStdioFile 吧。
读一行ReadString( LPTSTR lpsz, UINT nMax );

wltsuj 2003-08-21
  • 打赏
  • 举报
回复
while( szBuffer[i] != '\n' && !feof(pfile) )
{
i++;
szBuffer[i] = fgetc(pfile);
}
szBuffer[i+1]=''\0';

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧