C++ C++;需要从数组中查找符号吗
我需要创建一个程序来搜索.txt文件中有多少注释(C++ C++;需要从数组中查找符号吗,c++,if-statement,symbols,C++,If Statement,Symbols,我需要创建一个程序来搜索.txt文件中有多少注释(/符号) 这是我的代码: #include <fstream> using namespace std; const char read[] = "read.txt"; const char result[] = "result.txt"; const int CMax = 256; void Skaityti (char E[], int& n); int main() { char E[CMax]; int
/
符号)
这是我的代码:
#include <fstream>
using namespace std;
const char read[] = "read.txt";
const char result[] = "result.txt";
const int CMax = 256;
void Skaityti (char E[], int& n);
int main() {
char E[CMax];
int n,k;
Skaityti(E,n);
ofstream rs(result);
rs << k;
return 0;
}
void Skaityti (char E[], int & n)
{
ifstream fd(read);
int k;
char sim = '/';
for (n = 0; !fd.eof() && n < n+1; n++)
fd.get(E[n]);
for(int i = 0; i < n;i++) {
if(sim == E[n])
k++; }
fd.close();
}
#包括
使用名称空间std;
const char read[]=“read.txt”;
const char result[]=“result.txt”;
常数int CMax=256;
void Skaityti(字符E[],int&n);
int main(){
chare[CMax];
int n,k;
Skaityti(E,n);
流rs(结果);
rs我被你的问题弄糊涂了……但我想你是在问一个文件中出现了多少次“/”。我把这个放在一起:
#include <fstream>
using namespace std;
int Skaityti()
{
ifstream fd("test.txt");
int count = 0;
while(fd.good())
{
char c = fd.get();
if(fd.good())
if(c == '/')
{
c = fd.get();
if(fd.good())
if(c == '/')
count++; // At this point we have two comments in a row
}
}
fd.close();
return count;
}
#包括
使用名称空间std;
int Skaityti()
{
ifstream fd(“test.txt”);
整数计数=0;
while(fd.good())
{
char c=fd.get();
if(fd.good())
如果(c=='/'))
{
c=fd.get();
if(fd.good())
如果(c=='/'))
count++;//此时我们在一行中有两个注释
}
}
fd.close();
返回计数;
}
什么是“大规模”,它与你发布的代码有什么关系?如果你能提供一个你试图解析的文本、你期望的结果和你得到的结果的例子,那将非常有用。@OldProgrammer你没有大量的吗?我有。这是一个非常可爱的大量!我的错误,我想到了数组。不清楚问题是什么。