如何开始读取文件中文本的某些部分,一旦找到文本之前的关键字
How to start reading some part of the text in a file once a keyword preceeding the text is found
这是我写的代码…非常基本,因为我是初学者.....
源文件如下:
Integers:
1 2 3 4 56 ...
String:
This is a string......
...(text).....
代码应该根据开头遇到的关键字读取文本…
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int i;
string STRING;
char *inname = "source.txt";
ifstream infile(inname);
if (!infile)
{
cout << "There was a problem opening file "<< inname<< " for reading."<< endl;
return 0;
};
while (STRING != "Integer:")
{
getline(infile,STRING); // Saves the line in STRING.
cout<<STRING<<endl; // Prints our STRING.
};
};
cout << "Opened " << inname << " for reading." << endl<<endl<<"Integers:";
while (infile >> i) {
cout<<endl<<i<<endl;
infile.close();
return 0;
}
}
非常感谢!
您需要对该行中找到的单词进行标记。有很多方法可以做到这一点,您可以使用stringstream, boost tokenizer,甚至编写自己的代码。
假设您创建了一个规则,其中每个单词(标记)由空格分隔,并且您已经设法对行进行了标记,那么您可以编写代码检查该行中的第一个标记并适当地
恕我冒昧,您的解析器中缺少一些状态,一些检查文件是否仍然有效并解析整数。
这是一个概念证明:
#include <fstream>
#include <iostream>
#include <vector>
namespace {
void parse_integers(std::vector<int> & vi, std::string const & line) {
// Using strtol here - other tokenizers are possible.
char const * ip { line.c_str() };
char * ep;
do {
long int const v { strtol( ip, &ep, 10 ) };
vi.push_back( v );
ip = ep;
} while( *ep != ' ' );
}
}
int main() {
std::string inname { "source.txt" };
std::ifstream infile { inname };
if( ! infile ) {
std::cout << "There was a problem opening file "
<< inname << " for reading." << std::endl;
return 0;
};
enum class ParseState {
outer, found_integers, found_string
};
ParseState ps { ParseState::outer };
std::vector<int> s_integers;
std::string s_string;
while( infile ) {
std::string line;
getline( infile, line );
// Skip empty lines
if( line.size() == 0 ) {
continue;
}
if( line == "Integers:" ) {
ps = ParseState::found_integers;
continue;
} else if( line == "String:" ) {
ps = ParseState::found_string;
continue;
}
// Hope that a section was already found....
if( ps == ParseState::outer ) {
std::cerr << "Line with data but without a section found ["
<< line << "]" << std::endl;
continue;
}
switch( ps ) {
case ParseState::found_integers:
parse_integers(s_integers, line);
break;
case ParseState::found_string:
s_string += line + "n";
break;
case ParseState::outer:
// Cannot happen
abort();
}
}
std::cout << "Dump file contents" << std::endl;
std::cout << "Strings: [" << s_string << "]" << std::endl;
std::cout << "Integers: ";
for(int i : s_integers) {
std::cout << "[" << i << "] ";
}
std::cout << std::endl;
return 0;
}
输入:Integers:
1 2 3 4 56 111 761 777
String:
This is a string......
...(text).....
输出:Dump file contents
Strings: [This is a string......
...(text).....
]
Integers: [1] [2] [3] [4] [56] [111] [761] [777]
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- C++中的"inline"关键字
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何确保C++函数在定义之前声明(如override关键字)
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 读取文本文件,其中C++读取特定关键字后面的值
- 使用关键字从文本文件返回数字
- 在特定字段中关键字搜索后,从文本文件中提取多个记录
- 哪些 IDE 和文本编辑器可以推断出在 C++11 中使用 auto 关键字声明的变量类型
- 最快的方式做关键字搜索在大文本在C/ c++
- 如何开始读取文件中文本的某些部分,一旦找到文本之前的关键字