删除c/c++源程序中的注释
题目:编写一个程序,用于处理c/c++源程序,将源程序中的注释部分去掉
输入:c/c++文件名
输出:处理后的程序源文件
程序伪代码如下:
c1,c2:char
tag:int
a:读入一个字符存入c1
if tag==0 //读入字符不是注释
if c1=='/' //可能是注释标记
读入一个字符存入c2
if c2=='*' //为/**/注释的开头
tag = 2
else if c2=='/' //为//注释的开头
tag = 1
else //不是注释标记
将c1, c2保存
else //读入的字符为代码
保存c1
else if tag==1 //读入的是//注释
if c1=='\n' //为注释结束标记
tag=0
else //读入的是/**/注释tag==2
if c1=='*' //可能为注释结束标记
读入一个字符赋给c2
if c2=='/' //是/**/注释结尾
tag=0
else //注释没有结束
nothing
goto a
程序清单如下:
//对源文件进行扫描,删除其中的注释
#include <stdio.h>
void scan(char *filename)
{
int tag=0;//0:读取的字符为代码;1:读取的字符为//注释;2:读取的字符为/**/注释
char temp1, temp2;
FILE *in, *out;
in = fopen(filename,"r");
if(in==NULL)
{
printf("cannot open the input file!");
}
out = fopen("out.c", "w");
if(out==NULL)
{
printf("cannot open the output file!");
}
temp1 = fgetc(in);
while(temp1!=EOF)
{
printf("%c", temp1);
if(0==tag)
{
if('/'==temp1)
{
temp2 = fgetc(in);
if('*'==temp2)
{
tag = 2;
}
else if('/'==temp2)
{
tag = 1;
}
else
{
fputc(temp1, out);
fputc(temp2, out);
}
}
else
{
fputc(temp1, out);
}
}
else if(1==tag)
{
if('\n'==temp1)
{
tag = 0;
}
}
else
{
if('*'==temp1)
{
temp2 =
相关文档:
用了三种方法...
#if 0
void StringTokenize(const std::string& strSrc, const std::string& strDelimit, std::vector<std::string>& vecSub)
{
if (strSrc.empty() || strDelimit.empty())
{
throw "tokenize: empty string\n";
......
// & 与,将指定位置设置为0 | 或,将指定位置设置为1
//注: 只针对纯字母的情况
#include <stdio.h>
#include <string>
int main()
{
char str[6] = "xxing";
std::string str1 = "INGXX";
for(int i = 0; i < 5; i++)
{
str[i] &= 0xdf; ......
修改makefile,在LIBS里面加上-lmemcached,比如原来 gcc test.c,现在 gcc test.c -lmemcached。这个库就是libmemcached提供的。
然后添加#include<libmemcached/memcached.h>,这个文件也是libmemcached提供的。
主函数里面需要添加:
memcached_st *memc;
uint32_t&nbs ......
mmap是linux下的CreateFileMapping,用来映射并同步文件。
这样的话,比如我自定义一种文件格式,把它写入到文件中,现在想修改其中的值,就可以用这个函数,把文件映射到内存中
然后用操作数组的方式,来进行文件的同步。如果不用这个函数就得:
1、定义一个结构体
2、定义结构体数组
3、读取文件(w+)
4、修改文件 ......