【字符串处理以及文件读写】

由于长时间混用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);

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值