问一个在文本文件中找包含字符串行的问题

hdboy 2005-06-11 06:50:41
代码:
#include <iostream>
#include <fstream>

using namespace std;

int main(){
fstream fc;
char line[688][62];

fc.open("tel.dat");
for(int i=0;i<688&&!fc.eof();i++){
fc.getline(line[i],62);
cout<<line[i]<<endl;
}
fc.close();
return 0;
}

这样把文本里688行的内容一一送到line[i]中,我想在每一行中查找,只要里面包含有argv[1]的行就显示,否则不现实,然后按会车退出程序。这个程序应该怎么实现啊?
还有,文本文件里有中文,用中文做参数有问题么?
...全文
207 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hdboy 2005-06-12
  • 打赏
  • 举报
回复
to foochow(恰似你的温柔)
刚才没看到你的回帖,你的应该也可以,谢谢你啊
hdboy 2005-06-12
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <fstream.h>
#include <string.h>

using namespace std;

int main(int argc,char *argv[]){

if(argc!=2) cout <<"必须输入一个查询参数......";

fstream fc; //定义对象
char line[688][70]; //放文件内容的数组
fc.open("D:\\bin\\tel.dat"); //打开文件
for(int i=0;i<688&&!fc.eof();i++){ //循环到文件结束,文件共688行
fc.getline(line[i],70); //把文件的每一行赋给line数组

char temp[10]; //定义一个临时变量,如果没有它在bc++5编译会报错
strcpy(temp,line[i]); //copy字符串
if(strstr(temp,argv[1])){ //判断匹配参数

//if(strstr(&line[i],&argv[1])){
//cout<<line[i]<<endl;
cout<<temp<<endl; //输出匹配结果
}

}
fc.close(); //关闭文件
return 0;
}

这样可以用了。呵呵!
foochow 2005-06-11
  • 打赏
  • 举报
回复
//看看这个
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main(int argc,char *argv[]){
fstream fc;
char line[688][70];

fc.open("tel.dat");
for(int i=0;i<688&&!fc.eof();i++){
fc.getline(line[i],70);
char*p=line[i];
if(strstr(p,argv[1])){
cout<<line[i]<<endl;
}
}
fc.close();
return 0;
}
hdboy 2005-06-11
  • 打赏
  • 举报
回复
我用的是bcb5.5的命令行编译器bcc32,错误信息如下:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
I:\source\cpp\cha\cha.cpp:
Error E2285 I:\source\cpp\cha\cha.cpp 14: Could not find a match for 'strstr(char ( *)[70],char * *)' in function main(int,char * *)
Warning W8057 I:\source\cpp\cha\cha.cpp 18: Parameter 'argc' is never used in function main(int,char * *)
Warning W8057 I:\source\cpp\cha\cha.cpp 18: Parameter 'argv' is never used in function main(int,char * *)
*** 1 errors in Compile ***


程序如下:
#include <iostream.h>
#include <fstream.h>
#include <string.h>

using namespace std;

int main(int argc,char *argv[]){
fstream fc;
char line[688][70];

fc.open("tel.dat");
for(int i=0;i<688&&!fc.eof();i++){
fc.getline(line[i],70);
if(strstr(&line[i],&argv[1])){
cout<<line[i]<<endl;
}
}
fc.close();
return 0;
}
请各位高手帮忙。
hdboy 2005-06-11
  • 打赏
  • 举报
回复
那bc下应该怎么做呢?请高手指点!
ysbcg 2005-06-11
  • 打赏
  • 举报
回复
呵呵 他是用TC的或者vc的可能 所以才会有这种编译器不兼容现象
hdboy 2005-06-11
  • 打赏
  • 举报
回复
你好,请问strstr函数是在string.h头文件里定义的么?为什么我加入string.h后使用你教的方法,却报错呢?我用的是bcb5.5的命令行编译器bcc32,错误信息如下:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
I:\source\cpp\cha\cha.cpp:
Error E2285 I:\source\cpp\cha\cha.cpp 14: Could not find a match for 'strstr(char ( *)[70],char * *)' in function main(int,char * *)
Warning W8057 I:\source\cpp\cha\cha.cpp 18: Parameter 'argc' is never used in function main(int,char * *)
Warning W8057 I:\source\cpp\cha\cha.cpp 18: Parameter 'argv' is never used in function main(int,char * *)
*** 1 errors in Compile ***

修改后的程序如下:
#include <iostream.h>
#include <fstream.h>
#include <string.h>

using namespace std;

int main(int argc,char *argv[]){
fstream fc;
char line[688][70];

fc.open("tel.dat");
for(int i=0;i<688&&!fc.eof();i++){
fc.getline(line[i],70);
if(strstr(&line[i],&argv[1])){
cout<<line[i]<<endl;
}
}
fc.close();
return 0;
}

麻烦你了!
foochow 2005-06-11
  • 打赏
  • 举报
回复
mark!!
itr 2005-06-11
  • 打赏
  • 举报
回复
刚刚少打了一个&,应该是if(strstr(&line[i],&argv[1]))
itr 2005-06-11
  • 打赏
  • 举报
回复
在 fc.getline(line[i],62);
cout<<line[i]<<endl;
之间加一个if(strstr(&line[i],argv[1]))判断就成了。
用中文没有问题,只要你能在DOS下输入的都可以判断。

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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