尝试验证它的存在后,新行切除不工作 [英] Newline removal not working after trying to verify it exists

查看:110
本文介绍了尝试验证它的存在后,新行切除不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个code解析一些输入数据,并在一个特殊的格式写入文件。我从每个字符串删除换行符令牌像这样:

 令牌[strlen的(标记)-2] ='\\ 0'

-2 因为最后一个元素 \\ 0 。这么多的作品。然而,输入数据的最后一个元件不具有换行,所以仅使用该最终除去从最后输入集中的第二到最后一个字符。

 原始输入:0.38
通过消除运行之后:0.3

显而易见的解决方案是,以检查是否新行是有去除之前。我这样做:

 如果(令牌[strlen的(标记)-2] =='\\ n')令牌[strlen的(标记)-2] ='\\ 0';

然而,加入if子句后,换行不再删除!我究竟做错了什么?从整个code SNIPPIT:

 ,而((标记= strsep(安培;!行,))= NULL){
        如果(X ++ == 1){
            FWRITE(<项目> \\ N,11,1,FW);
            FWRITE(<名称>,14,1,FW);
            FWRITE(令牌,strlen的(标记),1,FW);
            FWRITE(< /名称> \\ N,8,1,FW);
        }
        否则如果(ISDIGIT(令牌[0])){
            如果(令牌[strlen的(标记)-2] =='\\ n')令牌[strlen的(标记)-2] ='\\ 0';
            FWRITE(< SPC>中,13,1,FW);
            FWRITE(令牌,strlen的(标记),1,FW);
            FWRITE(< / SPC> \\ N,7,1,FW);
            FWRITE(< /项目> \\ N,12,1,FW);
        }
    }


解决方案

您换行应该是行[strlen的(线)-1] ,但你可能下工作Windows其中一个换行实际上是由一个回车跟一个换行符。这可以解释为什么行[strlen的(线)-2] ='\\ 0'成功在去除结束线,但测试失败。

I am making a code to parse some input data and write it to a file in a special formatting. I am removing a newline from each string token as so:

token[strlen(token)-2] ='\0'

It is -2 because the last element is \0. This much works. However, the final element of the input data does NOT have a newline, so using just that ends up removing the second-to-last character from the last input set.

original input: 0.38
after running it through the removal: 0.3

The obvious solution is to check if the newline is there before removing. I do this:

if(token[strlen(token)-2] =='\n') token[strlen(token)-2] ='\0';

However, after adding the if clause, the newline is no longer removed! What am i doing wrong? Snippit from the whole code:

    while((token = strsep(&line, ",")) != NULL){
        if(x++ == 1){
            fwrite("    <item>\n", 11, 1, fw);
            fwrite("        <name>", 14, 1, fw);
            fwrite(token, strlen(token), 1, fw);
            fwrite("</name>\n", 8, 1, fw);
        }
        else if(isdigit(token[0])) {
            if(token[strlen(token)-2] =='\n') token[strlen(token)-2] ='\0';
            fwrite("        <SPC>", 13, 1, fw);
            fwrite(token, strlen(token), 1, fw);
            fwrite("</SPC>\n", 7, 1, fw);
            fwrite("    </item>\n", 12, 1, fw);
        }
    }

解决方案

Your newline should be at line[strlen(line)-1], but you may work under Windows where a newline actually consists of a carriage return followed by a newline. This would explain why line[strlen(line)-2]='\0' is successful in removing the end-of-line, but the test fails.

这篇关于尝试验证它的存在后,新行切除不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆