c++ 查找某个字符串所在行
答案:1 悬赏:50 手机版
解决时间 2021-04-06 05:13
- 提问者网友:雨不眠的下
- 2021-04-05 23:17
编写函数int find(string):已知string ID;这样一个字符串,需要建立一个函数,实现从text.txt中查找到相应的字符串,并把字符串所在行存到string Buff中供后续使用.找不到则返回0;
最佳答案
- 五星知识达人网友:有你哪都是故乡
- 2021-04-05 23:39
#include <string>
#include <istream>
#include <fstream>
using std::string;
using std::ifstream;
using std::istream;
//strFind是你查找的字符串
//strBuffer是返回的所在行
bool FindString(string strFind, string& strBuffer)
{
ifstream fTestTxt(_T("C:\\text.txt")); //the path of the file which you want to search
char oneLineBuffer[1024]; //one line has as many as 1024 character
while(fTestTxt.getline(oneLineBuffer, 1024))
{
string strTemp(oneLineBuffer);
if (string::npos != strTemp.find(strFind.c_str())) //find it
{
strBuffer = strTemp; //return buffer
return true;
}
}
return false;
}
#include <istream>
#include <fstream>
using std::string;
using std::ifstream;
using std::istream;
//strFind是你查找的字符串
//strBuffer是返回的所在行
bool FindString(string strFind, string& strBuffer)
{
ifstream fTestTxt(_T("C:\\text.txt")); //the path of the file which you want to search
char oneLineBuffer[1024]; //one line has as many as 1024 character
while(fTestTxt.getline(oneLineBuffer, 1024))
{
string strTemp(oneLineBuffer);
if (string::npos != strTemp.find(strFind.c_str())) //find it
{
strBuffer = strTemp; //return buffer
return true;
}
}
return false;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯