How to find a specific string in a text file?

How to find a specific string in a text file?
how i did it: i store the whole contents of the .txt file in a single
string, close the file, and then, split the string, based on searching a
specific character: a | bar.

text file test.txt:
helloooooooooo|cool cool|another string|laaaaast one|
the bar "|" is needed to separate words/sentences.

and main.cpp:

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 #include <fstream>
 5
 6  int main(){
 7     //load the text file and put it into a single string:
 8     std::ifstream in("test.txt");
 9     std::stringstream buffer;
10     buffer << in.rdbuf();
11     std::string test = buffer.str();
12     std::cout << test << std::endl << std::endl;
13
14     //create variables that will act as "cursors". we‘ll take everything between them.
15     size_t pos1 = 0;
16     size_t pos2;
17
18     //create the array to store the strings.
19     std::string str[4];
20
21     for (int x=0; x<=3; x++){
22         pos2 = test.find("|", pos1); //search for the bar "|". pos2 will be where the bar was found.
23         str[x] = test.substr(pos1, (pos2-pos1)); //make a substring, wich is nothing more
24                                               //than a copy of a fragment of the big string.
25         std::cout << str[x] << std::endl;
26         std::cout << "pos1:" << pos1 << ", pos2:" << pos2 << std::endl;
27         pos1 = pos2+1; // sets pos1 to the next character after pos2.
28                          //so, it can start searching the next bar |.
29     }
30  }

EDIT: and the output:

helloooooooooo|cool cool|another string|laaaaast one|

helloooooooooo
pos1:0, pos2:14
cool cool
pos1:15, pos2:24
another string
pos1:25, pos2:39
laaaaast one
pos1:40, pos2:52

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.

test.find("\\", pos3);

To find a "\" should use "\\".

时间: 2024-11-04 20:36:26

How to find a specific string in a text file?的相关文章

go read text file into string array

http://stackoverflow.com/questions/5884154/golang-read-text-file-into-string-array-and-write 方法一 1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "log" 7 "os" 8 ) 9 10 // readLines reads a whole file into memory 11 /

ffmpeg最全的命令参数

Hyper fast Audio and Video encoderusage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Getting help: -h -- print basic options -h long -- print more options -h full -- print all options (including all format and code

c++ 面试基本知识点整理(1)

目录 2. 什么是虚拟构造函数以及析构造函数 2.1 虚函数的实质 2.2 基类的析构函数必须用虚函数 2.3 以下函数不能使用虚函数 1)普通函数 2)友元函数 3)静态成员函数 4)构造函数,拷贝函数 5)内联函数: 3. 如何定义一个抽象类 2.引入原因 3.抽象类 4.抽象类的规定 6.虚函数和纯虚函数有以下所示方面的区别 7.抽象类与接口得区别 4. 拷贝构造函数的定义 4.1 拷贝构造函数的使用 4.2 使用场景 4.3 编译原理 4.4 浅拷贝与深拷贝 5. 重载与重写的区别 ov

downLoad

String root= ServletActionContext.getServletContext().getRealPath(File.separator).replace("\\", File.separator); String fileName = "download.text"; File file = new File(root + fileName); in = new FileInputStream(file); // 设置响应正文的MIME类型

几个数据库的小案例(一):将文本文件中的信息导入数据库的表中

从文本文件添加到数据库用户表的记录(有两个文件:frmMain.cs  SqlHelper.cs  ) //FrmMain.cs//作者:Meusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Fo

Oracle 11g导出来的dmp导入到 10g的数据库(IMP-00010:不是有效的导出文件,头部验证失败)

因为喜欢新的东西,所以基本上电脑的开发工具都是最新的,oracle也装了11g R2,但是同事同学不是11g R1就是10G的,从我这里导出去的dmp文件在他们那边导进去,都显示: IMP-00010:不是有效的导出文件,头部验证失败 为了这个问题一直苦恼,差点就想卸掉11g然后装10g了,后来想想,头部验证,那么头部到底是什么,用Notepad++查看了dmp文件,发现头部真的显示一些东西: 11g R2:V11.02.00 11g R1:V11.01.00 10g:V10.02.01 把版本

mysql插入文本文档及读取

1.把本地的一个文件插入到数据库中,数据库字段用text保存 public static void main(String[] args) { PropKit.use(“pro.txt”);DruidPlugin druid = new DruidPlugin(PropKit.get(“jdbcUrl”), PropKit.get(“user”), PropKit.get(“password”));druid.start();ActiveRecordPlugin arp = new Active

Create a Qt Widget Based Application—Windows

This turtorial describes how to use Qt Creator to create a small Qt application, Text Finder. It is a simplified version of the Qt UI Tools Text Finder Example. The application user interface is constructed from Qt widgets by using Qt Designer. The a

VB6-AppendToLog 通过API写入日志

工作中免不了需要为自己的程序添加日志,我也从网上扒拉了一个老外写的模块,修改修改了下,凑合用吧. 1 Option Explicit 2 '************************************** 3 ' 模块名称: AppendToLog 通过API写入日志 4 '************************************** 5 'API 声明 6 Private Const GENERIC_WRITE = &H40000000 7 Private Cons