由于长时间混用C和C++,以及自己C++学的不扎实,对类印象草草,简单的任务写了不短时间,特此整理一下
gets 读 char *会有问题,直接读char []
重定向freopen(, ,stdin); fclose(sdtin); 一般不太写fclose,我也不知道为啥
在尝试使用string的时候,遇到了读入问题和输出问题(具体是啥昨天刚写今天就忘记了。。。)还出现过bad_malloc什么的报错,emm
这种姿势是正确的
string s = "Hello World!";
printf("%s", s.c_str()); //输出 "Hello World!"
string.find_first_of("0x")也遇到过无法正确定位的情况,好像是直接定位到0第一次出现的位置
注意!以下写法是无法正确返回的
char * process(char s[])
{
char tmp[1000];
process...
return tmp;
}
这样才是可以的
char * process(char s[])
{
char tmp[1000];
process...
char *x=tmp;
return x;
}
看来我的习惯还是不好。。遇到的问题就这么让它散失在记忆的长河等我下次再遇到了
C++中string、char *、char[]、const char*的转换 太秀了,以前都没注意
代码在固定行会出现乱码,与文件内容无关,固定行乱码,很迷(已解决,将tmp数组开成全局即可,原因未知)
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
using namespace std;
char pre[1000];
char in[1000];
char tmp[1000];
char* process(char s[]) //传入一个字符串,返回要求的
{
//char tmp[1000];
int cnt=0;
int len=strlen(s);
for(int i=0;i<len;++i)
{
if(s[i]=='0'&&s[i+1]=='x')
{
while(i<len)
{
if(s[i]=='/'||(s[i]=='U')&&s[i+1]=='N') break;
if(s[i]=='+')
{
tmp[0]='\0';
break;
// char *x=tmp;
// return x;
}
tmp[cnt++]=s[i];
i++;
}
tmp[cnt]='\0';
break;
}
}
char *re=tmp;
return re;
}
int main()
{
char fname[1000];
gets(fname); //读char *会出问题
const char* fin=fname;
freopen(fin,"r",stdin);
FILE *fp=fopen("out14.txt","w");
while(gets(in))
{
char *x=process(in);
if(x[0]=='\0') continue;
else
{
fprintf(fp,"%s\n",x);
}
}
fclose(stdin);
fclose(fp);
}