求一段小代码

金来 2003-09-04 09:49:39
从文件A中距离文件首POS(long型)处读取含有ABCD的字符串,并输出这些行.直到文件结束.
...全文
47 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
to Smallest(常住了)
>楼上的有点偏离我的意思了,输出的是行,不是字符串

1: 如果要按行输出,把istream_iterator换成line_iterator就可以了。
2: line_iterator可以参见Matthew H. Austern的<<Generic Programming and STL>>.

>还有,我忘记说环境了:LINUX GCC
3: 我是用STD C++写得,所有C++编译器都可,和平台无关。
4: 只出现一处跟平台关联的地方:
istr("C:\\test.txt");
这只是为了举例,你可以随便改成什么样子,比如
istr("./test.txt");

5: this solution is definitely the very C++ way.
6: 当然我的东西只是一种解答,大家共同学习。
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
to Smallest(常住了)
>楼上的有点偏离我的意思了,输出的是行,不是字符串

1: 如果要按行输出,把istream_iterator换成line_iterator就可以了。
2: line_iterator可以参见Matthew H. Austern的<<Generic Programming and STL>>.

>还有,我忘记说环境了:LINUX GCC
3: 我是用STD C++写得,所有C++编译器都可,和平台无关。
4: 只出现一处跟平台关联的地方:
istr("C:\\test.txt");
这只是为了举例,你可以随便改成什么样子,比如
istr("./test.txt");

5: this solution is definitely the very C++ way.
6: 当然我的东西只是一种解答,大家共同学习。
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
不知为何:
这一句没有生效:
istr.seekg(20, std::ios_base::beg);

暂时简单替换为:
int i = 0;
while (++i <= 20 && istr)
istr.get();
//istr.seekg(20, std::ios_base::beg);

结果中前20字节里的ABCD没有了:

/**
* console output
ABCD
111ABCD
1ABCD222
ABCD222
ABCDEEEF
*/

金来 2003-09-04
  • 打赏
  • 举报
回复
楼上的有点偏离我的意思了,输出的是行,不是字符串
还有,我忘记说环境了:LINUX GCC
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
/**
* 从文件A中距离文件首POS(long型)处读取含有ABCD的字符串
* 并输出这些行.直到文件结束.
*/
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <algorithm>

namespace
{
struct remove_cmp
{
remove_cmp(char const* s)
: target(s)
{}

bool operator () (std::string const& s1) const
{
return std::string::npos == s1.find(target); // not find, remove it
}

private:
char const* target;
};
}

void print_matched_strs(std::istream& s, char const* target)
{
using namespace std;
istream_iterator<string> begin(s);
istream_iterator<string> end;
remove_copy_if(
begin, end,
ostream_iterator<string>(cout, "\n"),
remove_cmp(target)
);
}

int main()
{
std::ifstream istr("c:\\test.txt");
istr.seekg(20, std::ios_base::beg);
print_matched_strs(istr, "ABCD");
}

/**
* c:\test.txt
asdf ABCD
asdf asdf asdf
ABCD
asdfasdf 111ABCD 1ABCD222 ABCD222
ASDFASDF
ABCDEEEF
*/

/**
* console output
ABCD
ABCD
111ABCD
1ABCD222
ABCD222
ABCDEEEF
*/
mixtrue 2003-09-04
  • 打赏
  • 举报
回复
bigbigbigsoft(bigbigbigsoft) 兄弟,如果我的文件超过10000 怎么办,如果这样就会出现
内存泄漏问题。


搂主可以用c plus plus 中的摸版阿。现提供如下
TextReader.readLine

Reads a line.

Syntax

public String readLine()

Return Value

Returns the next line from the input stream, or returns null if the end of the input stream is reached.

Remarks

A line is defined as a sequence of characters followed by a carriage return (\r), a line feed (\n), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.


搂住可以一行一行的读出,然后对该行进行处理。
金来 2003-09-04
  • 打赏
  • 举报
回复
好像有错!
bigbigbigsoft 2003-09-04
  • 打赏
  • 举报
回复
file = fopen(A, "r")
fseek(file, POS, SEEK_SET);
while(!EOF(file))
{
char buffer[10000];
fgets(buffer, 10000, file);
if(strstr(buffer, "ABCD"))
{
printf("%s\n", buffer);
}
}
ezhou 2003-09-04
  • 打赏
  • 举报
回复
猛!

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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