c++ - 在 C++ 中将字符与字符串的组成部分进行比较时出现错误

标签 c++ string file

#include <iostream>
#include <fstream>
#include <cstring>
#define MAX_CHARS_PER_LINE 512
#define MAX_TOKENS_PER_LINE 20
#define DELIMITER " "
using namespace std;
int main ()
{
    //char buf[MAX_CHARS_PER_LINE];
    string buf; // string to store a line in file
    fstream fin;
    ofstream fout;
    fin.open("PiCalculator.h", ios::in);
    fout.open("op1.txt", ios::out);
    fout<< "#include \"PiCalculator.h\"\n";
    static int flag=0; //this variable counts the no of curly brackets

    while (!fin.eof())
    {
        // read an entire line into memory
        getline(fin, buf);
        //fout<<buf.back()<<endl;
        if(buf.back()== "{" && buf.front() !='c'){
            flag++;
            fout<<buf<<endl;
        }
        if(flag > 0)
            fout<<buf<<endl;
        if(buf.back()== "}"){
            flag--;
            fout<<buf<<endl;
        }
    }
    cout<<buf.back()<<endl;
    return 0;
}

这里我在 if 条件中遇到错误:

if(buf.back()== "{" && buf.front() !='c')

错误指出:ISO C++ 禁止指针和整数之间的比较 [-fpermissive]

谁能帮我解决这个问题吗?

最佳答案

由于您检查了 bug.back 的“{”和 bug.front 的“c”,因此您的比较可能无效。我假设 back 和 front 函数返回的类型必须相同。一个是单引号,另一个“{”是双引号。这就是问题所在。

将“{”更改为“{”

希望这有帮助。

关于c++ - 在 C++ 中将字符与字符串的组成部分进行比较时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403421/

相关文章:

c++ - 查找具有给定总和的子数组

c++ - 如何从 C++ 中的线程访问非静态方法使用 ref 类

regex - 使用 strsplit 获取 r 中的最后一个字符

java - JVM和Java是如何实现文件读写的?

c++ - 在函数中使用 void。认不出来

c++ - 给定结构的大小

c# - 在 C# 中将字符串转换为 int

php - 查找字符串中出现的字符串并替换 - php

c++ - 如何将 ifstream 返回到刚刚在 C++ 中读取的行的开头?

c - fscanf 不从文件读取