在文本文件中找到特定字符

Find a particular character in a text file?

本文关键字:字符 文本 文件      更新时间:2023-10-16

我有一个文本文件,如下所示:

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;
}