English 中文(简体)
打印字符串( Char*) 从文件中装入, 且没有逃避的特殊序列( 如 )
原标题:Printing strings (char*) loaded from file without escaped special sequences (like )

我目前正在为我的玩具语言写一个解析器, 作为这个解析器的一部分, 我有一个书面打印功能... 基本上是打印它的论点。 对于字符串常数, 它所做的就是

printf("%s", pointer);

so so, so, so, so, so, so, so, so, so,

print("
")

应于下列条件下执行:

printf("%s", ptr_to_loaded_string);

(或多或少)

However, my current problem is, that C escapes special character sequences while reading script file. So instead of " " I get "\n".

我的问题是:我是否可以避免这些序列的逃脱,如果不是最好的处理方法是什么?我目前正在考虑搜索和替换——将每个序列中的2个替换为1个,但可能有点问题(时间长度变化、真实行距等)――除非绝对必要,否则我想避免这一解决方案。

编辑: 啊哈, 堆积流逃过了我的举例....

最佳回答

It s not that C is un-escaping your sequences -- it s that it s simply leaving them alone, so your " " in the input stream is read as two characters ( and n ).

以下是我先前写下的“em>years years ” 来解决这个问题的一些代码:

/*
** Public Domain by Jerry Coffin.
**
** Interpets a string in a manner similar to that the compiler
** does string literals in a program.  All escape sequences are
** longer than their translated equivalant, so the string is
** translated in place and either remains the same length or
** becomes shorter.
*/

#include <string.h>
#include <stdio.h>
#include "snip_str.h"

char *translate(char *string)
{
      char *here=string;
      size_t len=strlen(string);
      int num;
      int numlen;

      while (NULL!=(here=strchr(here, \ )))
      {
            numlen=1;
            switch (here[1])
            {
            case  \ :
                  break;

            case  r :
                  *here =  
 ;
                  break;

            case  n :
                  *here =  
 ;
                  break;

            case  t :
                  *here =  	 ;
                  break;

            case  v :
                  *here =  v ;
                  break;

            case  a :
                  *here =  a ;
                  break;

            case  0 :
            case  1 :
            case  2 :
            case  3 :
            case  4 :
            case  5 :
            case  6 :
            case  7 :
                  numlen = sscanf(here,"%o",&num);
                  *here = (char)num;
                  break;

            case  x :
                  numlen = sscanf(here,"%x",&num);
                  *here = (char) num;
                  break;
            }
            num = here - string + numlen;
            here++;
            memmove(here,here+numlen,len-num );
      }
      return string;
}
问题回答

您不能有 C 风格的特殊字符从字符序列中直接解释 C 样式的特殊字符( 例如从输入文件文件) 。 您需要写入解析逻辑以确定该序列是否包含所需的特殊字符序列并按此处理

note :确保您也妥善处理逃跑字符。

如果您愿意使用 GLib, 您可以 < a href=" http:// developmenter.gnome.org/glib/2.28/glib- String- 公用事业- Functions.html# g- strcompress" rel = "nofollow" >g_strcompress 您的字符串可以转换逃跑的字符, 然后打印结果 。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签