【发布时间】:2014-05-01 06:56:03
【问题描述】:
#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]
谁能帮我解决问题??
【问题讨论】:
-
首先需要
#include <string>。其次,你需要阅读some documentation onstd::string。 -
第三,这个:
while (!fin.eof())is wrong。 -
第四,在 C++ 中使用预处理器
#defines 几乎从来都不是好事。阅读const和 (C++11)constexpr。 -
@WhozCraig:为什么 while(!fin.eof()) 是错误的?
-
@cryptoaniac60 位于评论中提供的链接中。