心力交瘁,帮看看行号哪里错了?

starcat 2010-05-03 01:13:21
Create a program that opens a file (the first argument on the command line) and searches it for any one of a set of words (the remaining arguments on the command line). Read the input a line at a time, and print out the lines (with line numbers) that match.
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {
ifstream in(argv[1], ios::binary);
in.seekg(0, ios::end);
long fileSize = in.tellg();
cout << "file size = " << fileSize << endl;
string line, sch(argv[2]);
int linecount = 0;
cout << sch << "\n";
in.seekg(0, ios::beg);
while(getline(in, line)) {
++linecount;
if(line.find(sch) != string::npos)
cout << linecount << ": "
<< line << endl;
}
in.close();

return 0;
}

...全文
220 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我调试了也没错,只是不知楼主需要实现的是怎么样的一个效果...
cattycat 2010-05-03
  • 打赏
  • 举报
回复
逻辑应该没问题。
jixingzhong 2010-05-03
  • 打赏
  • 举报
回复
应该是正确的...
fox000002 2010-05-03
  • 打赏
  • 举报
回复
试着编译执行了一下,没看出有什么问题
logiciel 2010-05-03
  • 打赏
  • 举报
回复
如何理解"searches it for any one of a set of words (the remaining arguments on the command line)"?

假定输入文件有以下内容并要求搜索"on the command line":

Create a program that opens a file (the first argument on the command line)
and searches it for any one of a set of words (the remaining arguments on the
command line). Read the input a line at a time, and print out the lines
(with line numbers) that match.

.LZ程序只找到第1行,不能找到第2、3行中的"on the command line"。
starcat 2010-05-03
  • 打赏
  • 举报
回复
在一个文件(argv[1])中查找特定的字符串(argv[2],含空格的窜用""括起来)
结果中输出的行号不对
fox000002 2010-05-03
  • 打赏
  • 举报
回复
这个有什么错误吗???
starcat 2010-05-03
  • 打赏
  • 举报
回复
被查找string所在的行号

64,282

社区成员

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

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