在文本文件中找到特定字符
Find a particular character in a text file?
我有一个文本文件,如下所示:
FullAdder
3输入
输入(a)
输入(b)
输入(CIN)
现在,我需要知道在这里" a"," b"answers" cin"的输入名称。我可以打开文件并阅读。但是我正在变得奇怪的输出。我的代码如下:
// Finding the names of inputs
if(buffer[i] == 'I' && buffer[i+1]=='N' && buffer[i+2]=='P' && buff er[i+5]=='(')
{
if( buffer[i+7]==')' ) // i.e. single digit Input name
{
char inputName[1];
inputName[0] = buffer[6];
string name(inputName);
cout<<"n Name of inputs: "<<inputName[0]<<"n";
}
else if( buffer[i+8]==')' ) // To check is the value of value of inputs consists of one digit or two digit
{
char inputName[2];
inputName[0] = buffer[6]; inputName[1] = buffer[7];
string name(inputName);
cout<<"n Name of inputs: "<<inputName[0]<<inputName[1]<<"n";
}
else if( buffer[i+9]==')' ) // i.e. three digit Input name
{
char inputName[3];
inputName[0] = buffer[6]; inputName[1] = buffer[7]; inputName[2] = buffer[8];
string name(inputName);
cout<<"n Name of inputs: "<<inputName[0]<<inputName[1]<<inputName[2]<<"n";
}
}
,但我得到以下输出:
输入的名称:a
输入的名称:a
输入的名称:add
从缓冲区复制字符时,您会忘记被i
的偏移。例如:
inputName[0] = buffer[6];
应该是:
inputName[0] = buffer[i+6];
#include <iostream>
#include <string>
using namespace std;
string getpart(string &input){
string pre("INPUT(");
string ret("");
size_t len = pre.length();
size_t pos = input.find(pre);
if(pos == 0){
pos = input.find(")", len);
ret = input.substr(len, pos - len);
}
return ret;
}
int main() {
string input, part;
for(int i=0; i<3 ; ++i){
cin >> input;
part = getpart(input);
cout << part << endl;
}
}
#include <iostream>
#include <string>
using namespace std;
string getpart(string &input, const char *pre, const char *post){
string ret("");
string::size_type pre_pos = input.find(pre), len = string(pre).length();
if(pre_pos != string::npos){
pre_pos += len;
string::size_type post_pos = input.find(post, pre_pos);
if(post_pos != string::npos)
ret = input.substr(pre_pos, post_pos - pre_pos);
}
return ret;
}
int main() {
string input, part;
getline(cin, input);
part = getpart(input, "INPUT(", ")");
cout << part << endl;
}
看起来您的计数器" i"可能代表了当前行的开头,但是当您实际上将字符从数组中拉出时,您正在从固定索引中读取。
例如,对于一个字符案例,您应该从" buffer [i 6]"而不是" buffer [6]"中读取。
话虽如此,我非常强烈建议您编写一些功能来弄清楚如何解析行,以便您不必为值的每一个可能的长度编写代码。
@user2440724-关于您的问题:例如,查看(运行)使用strtok()
的小例子。介入一个突破点,并查看BUF的价值:
#include <ansi_c.h>
int main(void)
{
char buffer[]="INPUT(A)"; //simulated line buffer
char *buf;
buf = strtok(buffer, "(");//strip away INPUT and (
buf = strtok(NULL, ")");//Capture contents of ( )
//do something with "A", read next line buffer
return 0;
}
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何从文本文件中读取数值,直到遇到字符类型?
- 如何在C++中反转文本文件中字符的顺序?
- 如何从代码本身向 wxwidgets 中的文本控件插入字符?
- 无法将整个文本文件复制到字符数组
- 在处理任何字符大小的模板中使用字符串文本
- 从我的 2DgridArray 中的文本文件中获取每个字符
- 从文本文件 (C++) 生成字符矩阵
- 如何在给定文本中用字符数组替换符号
- 如何在文本文件中间插入字符
- 将字符串文本分配给字符*:const_cast<字符 *> 与使用 char 数组
- 如何从文本文件中一次读取一个字符
- 字符* 文本消息[] 如何在内存中格式化?
- VS2013 字符文本不能作为 MSDN 的规范工作
- 为什么 C 中的字符文本大小与 C++ 中的字符文本大小不同
- 以本地化安全的方式比较C++中的字符串/字符和字符串/字符文本
- 在块中分配字符文本"if"
- C++编译时将字符串文本转换为多字符文本
- C++将字符串文本添加到字符文本